Solved

when incremented object value is 1

Posted on 2014-03-29
8
306 Views
Last Modified: 2014-03-31
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
0
Comment
Question by:rgb192
  • 4
  • 4
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39963969
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
 

Author Comment

by:rgb192
ID: 39964908
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39965075
$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
 

Author Comment

by:rgb192
ID: 39965106
$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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39965125
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
 

Author Comment

by:rgb192
ID: 39965416
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39965427
The reason you see a Notice is because the variable is not set.  That's the point -- it's a mistake!
0
 

Author Closing Comment

by:rgb192
ID: 39966869
// 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now