2

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

3
  • 5
    The += operator is shorthand. $x += $t is shorthand for $x = $x + $t. As you can see, $x isn't defined, so you can't use it in the equation. Commented Jan 2, 2015 at 20:11
  • 1
    I will only point out the possibility to suppress the error with @$x += $t;. Encourage you not to do so. Commented Jan 2, 2015 at 20:20
  • 1
    Then why point it out @Laxus? Commented Jan 2, 2015 at 20:21

2 Answers 2

6

$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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@user3238082 If you have a new question make a new one! So you get the full attention of every one :D Otherwise if you miss wrote you in your question you can edit it, and if it's changing large parts of the question mark it as an edit! Otherwise people will come by see the answers and the question and think that the answers are wrong because they don't match the question anymore!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.