Solved

when incremented object value is 1

Posted on 2014-03-29
8
297 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
Comment Utility
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
Comment Utility
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
Comment Utility
$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
Comment Utility
$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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
// 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

743 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

15 Experts available now in Live!

Get 1:1 Help Now