0

I have a weird problem. I try to add two strings together but instad the variable evaluates to 0. Why is this happening and how to fix it ?

<?php
    require_once('config.php');
    $connect->query("SELECT id, name FROM room");
    $rooms = $connect->get();
    print_r($rooms);
    $result = "<ul>";
    $asd = "Test";
    /*
    foreach($rooms as $room ){
        $result += $result + "<li>". $room['name'] ."</li>";
        print_r($result);
    }*/

    $result += $result + "</ul>";
    $asd =  $asd + "AAAA";
    print_r($asd);
    //print_r($result);

    //print_r($asd) outputs 0
3
Contributors
2
Replies
10
Views
4 Years
Discussion Span
Last Post by IIM
1

String concatenation uses a . not a +

A plus is for number addition, so the zero results from a conversion of the string to a number.

1

For String concatenate use "." .

If you use +,it considers it as numeric and if it is not a valid number it will return 0.

If the start of the string is a valid numeric representation, use that until we come across a character incompatible with that format
Otherwise return zero

The (int) cast is actually unnecessary, PHP will automatically handle this when you pass the value to a function that expects an integer.

Edited by IIM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.