Beach_Boy
Beach_Boy

Reputation: 37

undefined variable when using assignment operators(+=)

i have trouble in codeigneter when using assignment operators (+=). Please help me.

Here my code in view:

<?php
$t = 220; 
$x += $t;

echo $x;
?>   

i get the result but in my view there have a error mesage.

A PHP Error was encountered:

Severity: Notice Message: Undefined variable: x

Upvotes: 2

Views: 458

Answers (2)

ka_lin
ka_lin

Reputation: 9442

So define it:

<?php
    $x = 0;
    $t = 220; 
    $x += $t;    
    echo $x;
    ?>

You are telling the code to add to $x a number, this $x is not defined at that point.

Upvotes: 1

Rizier123
Rizier123

Reputation: 59701

$x is not initialized so just do this:

<?php

    $t = 220;
    $x = 0;

    $x += $t;

    echo $x;

?>

Output:

220

Side Note:

You can add error reporting at the top of your file to get error messages (ONLY in testing environment):

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

Upvotes: 6

Related Questions