how to put 2+ PHP echo's on the same line?

Discussion in 'Programming' started by Digger, Apr 29, 2018.

  1. I was wondering how to put 2+ PHP echo's on the same line
  2. Why would you need two echo's, why not just combine the thing you need to echo into one?
  3. It's because I'm checking a SQL-DB and seeing if they have a role and if they have more then one I need it on the same line
  4. Again, I'm really not seeing why this can't be put into one echo.


    //May not work, I don't know php.
        $address = "ip";
        $username = "uername";
        $password = "password";
        $dbname = "dbname";

        try {
            $conn = new PDO("mysql:host=$address;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $getRoles = $conn->prepare("query");

            $roles = $getRoles->fetchAll();

            $roleString = "";

            foreach ($roles as $role) {
                $roleString = $roleString . " " . $role['column-containing-role-name'];

            echo roleString;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();

        $conn = null;

    • Agree Agree x 1
  5. md_5

    Administrator Developer

    echo "echo1"; echo "echo2";

    • Like Like x 2
  6. Strahan


    First of all, an apostrophe has only few very specific uses in pluralization, and "echo's" is not one of them.

    Secondly, yea, I'm not understanding your issue. If you want more than one thing on the line, just don't echo a CR. Subsequent echos will be on the same line by default. I.E:

    Code (Text):
    $data = array("hi", "my", "name", "is", "Strahan");
    foreach ($data AS $word) echo "$word ";

    Code (Text):
    hi my name is Strahan
    If I wanted it to work otherwise, I'd have to throw \n in there so it'd do linebreaks.
  7. Or
    Code (Text):
    echo "echo1" . "echo2";
  8. I figured it out by myself, I made a modal that was invisible