[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

when incremented object value is 1

Posted on 2014-03-29
8
Medium Priority
?
342 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 111

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
 
LVL 1

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 111

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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LVL 1

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
 
LVL 111

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
 
LVL 1

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
 
LVL 1

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

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

607 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