Solved

when incremented object value is 1

Posted on 2014-03-29
8
310 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 109

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 109

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 109

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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 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

10 Experts available now in Live!

Get 1:1 Help Now