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

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.