when incremented object value is 1

from Ray code tutorial. Which I edit to understand.

output:
Notice: Undefined property: stdClass::$89 in C:\wamp\www\variables-references\7-array-and-object-variables-in-iterators2.php on line 13

obj->val:
Notice: Undefined property: stdClass::$9 in C:\wamp\www\variables-references\7-array-and-object-variables-in-iterators2.php on line 20

Notice: Undefined property: stdClass::$14 in C:\wamp\www\variables-references\7-array-and-object-variables-in-iterators2.php on line 21

Notice: Undefined property: stdClass::$a in C:\wamp\www\variables-references\7-array-and-object-variables-in-iterators2.php on line 22
stdClass Object ( [Z] => 26 [B] => 200 [C] => 3 [89] => 0 ) 

Open in new window


<?php
$obj = new stdClass;
$obj->A = 10;
$obj->Z = 26;
$obj->B = 200;
$obj->C = 3;

foreach ($obj as $name => $value)
{
    if ($name == 'A') unset($obj->$name);
    if ($name == 'B') {
      $value=89;
      echo '<br>obj->val:'.$obj->$value;
      $obj->$value++;
      $obj->$value--;
      //($obj->$value)++;
      $nine="9";
      $fourteen="14";
      $a="a";
      $obj->$nine;
      $obj->$fourteen;
      $obj->$a;
    }
}
print_r($obj); // stdClass Object ( [B] => 3 [C] => 3 )

Open in new window



Line 14: value of $obj->$value contains no value, but when incremented becomes 1
I do not understand

if I decrement first: $obj->$value is null not -1
$obj->$value is not 0

I even set $value=89 which is only $name of object
LVL 1
rgb192Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
The reason you see a Notice is because the variable is not set.  That's the point -- it's a mistake!
0
 
Ray PaseurCommented:
I don't think you want to use representations of numbers as the names of object properties.  That might even be illegal in PHP.

Let's start with this.  Have a look and see if it makes sense to you, then tell me what you want to change next.
http://www.iconoun.com/demo/temp_rgb192.php

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

$obj = new stdClass;
$obj->A = 10;
$obj->Z = 26;
$obj->B = 200;
$obj->C = 3;

// VISUALIZE THE ENTIRE OBJECT
var_dump($obj);

// ITERATE OVER THE OBJECTG
foreach ($obj as $name => $value)
{
    // BEFORE ANYTHING ELSE, VISUALIZE THE NAME AND VALUE
    echo PHP_EOL . "$name => $value";

    // IF NAME IS 'A' REMOVE THE ELEMENT FROM THE OBJECT
    if ($name == 'A') unset($obj->$name);

    // IF NAME IS 'B' ADD ONE TO THE VALUE
    if ($name == 'B')
    {
        $obj->$name++;
    }
}

// SHOW THE MODIFIED OBJECT
echo PHP_EOL;
print_r($obj);

Open in new window

0
 
rgb192Author Commented:
nameB changed from value200 to value201

    // IF NAME IS 'B' ADD ONE TO THE VALUE
    if ($name == 'B')
    {
        $obj->$name++;
    }


I don't think you want to use representations of numbers as the names of object properties.  That might even be illegal in PHP.

I still do not understand the error I got with $obj->$value

how can I recreate

I tried setting $value and attaching to object but it only happened once.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
$value in the foreach() iterator is a copy of a variable value from the object.  The name of the variable is something like $obj->A.  The contents of $obj->A is the integer 10.
0
 
rgb192Author Commented:
$obj->A=10
name is A
Value is 10
contents is 10




foreach ($obj as $name => $value)
{
    if ($name == 'A') unset($obj->$name);
    if ($name == 'B') {
      $value=89;
      echo '<br>obj->val:'.$obj->$value;
      $obj->$value++;

when $name==B
$obj->$B
name is B
value is 89
contents is 89
I do not understand this: $obj->$value ($obj->89????) is null until incremented and becomes 1
0
 
Ray PaseurCommented:
Install this script on your server and run it.  Look at each line of code and each line of output.  Then go back up to line 2 and remove the comment marker so that the error_reporting() statement is in effect.  When you run it again you will see different output.

<?php // demo/temp_rgb192.php
// error_reporting(E_ALL);
echo '<pre>';

$obj = new stdClass;
$obj->A = 10;
$obj->Z = 26;
$obj->B = 200;
$obj->C = 3;

// VISUALIZE THE ENTIRE OBJECT
var_dump($obj);

// ITERATE OVER THE OBJECTG
foreach ($obj as $name => $value)
{
    // BEFORE ANYTHING ELSE, VISUALIZE THE NAME AND VALUE
    echo PHP_EOL . "$name => $value";

    // IF NAME IS 'A' REMOVE THE ELEMENT FROM THE OBJECT
    if ($name == 'A') unset($obj->$name);

    // IF NAME IS 'B'
    if ($name == 'B')
    {
        // OVERWRITE THE CONTENTS OF THE $value VARIABLE
        //   THIS ONLY CHANGES THE LOCAL COPY.  NOTHING CHANGES IN THE OBJECT
        $value=89;

        // THIS ECHO STATEMENT IS CONFUSING AND POTENTIALLY MISLEADING
        //   $obj->val IS NOT THE SAME AS $obj->value OR $obj->name OR $obj->$name OR $obj->$value
        //   $obj->$value IS AN UNDEFINED VARIABLE BUT YOU WILL
        //   NEVER KNOW THIS UNLESS YOU RAISE THE DEFAULT PHP ERROR REPORTING LEVEL
        //   YOU SHOULD ALWAYS PROGRAM WITH PHP ERROR_REPORTING(E_ALL)
        echo '<br>obj->val:' . $obj->$value;

        // THIS USES AN ARTIFACT OF PHP LOOSE DATA TYPING TO MAKE A "FALSY" VALUE INTO ZERO
        //   THEN AFTER CONVERTING THE UNDEFINED "FALSY" VALUE TO ZERO, IT ADDS ONE.
        $obj->$value++;
    }
}

// SHOW THE MODIFIED OBJECT
echo PHP_EOL;
print_r($obj);

Open in new window

0
 
rgb192Author Commented:
first line 35
$obj->$value
name:89 value:1


object(stdClass)#1 (4) {
  ["A"]=>
  int(10)
  ["Z"]=>
  int(26)
  ["B"]=>
  int(200)
  ["C"]=>
  int(3)
}

A => 10
Z => 26
B => 200
Notice:  Undefined property: stdClass::$89 in C:\wamp\www\variables-references\7c.php on line 35
obj->val:
C => 3
89 => 1
stdClass Object
(
    [Z] => 26
    [B NOT BOLD] => 200
    [C] => 3
    [89] => 1
)






















-----------------------
Notice:  Undefined property: stdClass::$89 in C:\wamp\www\variables-references\7c.php on line 35

I see a notice now but do not understand how this variable is set
0
 
rgb192Author Commented:
// THIS USES AN ARTIFACT OF PHP LOOSE DATA TYPING TO MAKE A "FALSY" VALUE INTO ZERO
        //   THEN AFTER CONVERTING THE UNDEFINED "FALSY" VALUE TO ZERO, IT ADDS ONE.
        $obj->$value++;


I do not fully understand but now know keywords 'loose data typing'  and a notice of variable not set and false value to 0 adding 1

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.