0

I wanna know how php interprets this code. Is it something of syntax error or matching priorty? I appreciate any help.
Code:

// php 5.3
class Man {
    var $arr = array('name'=>'me');
}

$key = 'name';
$man = new Man();
echo $man->arr['name']; // output me
echo $man->$key['name']; // output nothing along with warning and notice

// output:
PHP Warning:  Illegal string offset 'name' in php shell code on line 1
PHP Notice:  Undefined property: Man::$n in php shell code on line 1
2
  • 1
    $key is name. There is no property name in Man class. That's why there's a warning. Also the above looks like PHP 4 syntax. please try to avoid it, your co-workers will be grateful. Commented Jul 24, 2018 at 9:25
  • PHP 7 have another output (Notice: Undefined property: Man::$name): 3v4l.org/o5sgS Commented Jul 24, 2018 at 9:29

5 Answers 5

3

Well, do you mean $key['name'] or $man->$key and ['name'] thereof? It's ambiguous syntax and PHP is interpreting it the former way, trying to get index 'name' of string 'name', resulting in a warning being output and the interpretation being 'name'[0], i.e. 'n'.

Disambiguate the syntax:

$man->{$key}['name']
Sign up to request clarification or add additional context in comments.

4 Comments

@mover if you think this answer solved your problem, mark it as accepted by clicking the gray check icon.
It's some kind of Complex (curly) syntax. Here is the link:php.net/manual/en/…
I think it is trying to get index 'name' of string 'n' not string 'name'. When you don't provide brackets it only considers the first character of the string.
@Pooya "Illegal string offset 'name'" → it's trying to get the offset ['name'] of a string, clearly meaning it's trying $key['name'] where $key is the string. Since 'name' is an invalid/illegal offset of a string, it produces this warning. This is then falling back to interpreting the offset 'name' as the next best numeric offset, which ends up being 0 ((int)'name'0). So it results in $key[0]. Which then translates to $man->n and "Undefined property: Man::$n". See 3v4l.org/MFmgf.
0

This doesn't translate to anything because you're trying to access a property with a tag of a variable. It's not a variable, it won't work:

echo $man->$key['name'];

This on the other hand will work:

echo $man->{$key}['name'];

This lets php know the $key you're about to used is actually a property wrapped in {}

2 Comments

Thanks so much, César Ferreira.
Glad I could help ;)
0

There is no name property define in your class. Because echo $man->$key['name']; convert to echo name['name];

Comments

0
class Man {
    var $arr = array("name"=>"me");
}

$key = 'arr';
$man = new Man();
echo $man->arr['name']; // output me
echo $man->{$key}['name']; // output nothing along with warning and notice

Look how i used square brackets around $key. When you don't provide brackets it interprets to $key[0] :

echo $man->a but you can not access array values.

3 Comments

I think your explaination is not right here. $this->$key['name'] will interpret to "name['name']", but name doesn't have index 'name', so instead it return "name[0]", that is 'n'. Pls refer to the answer of deceze.
@Max look at my own example. $key = 'arr' And $arr already has index name but still php interprets $key to $a. just run my example without brackets and see the results. I hope you get it this time. just tell me if anything is unclear.
Sorry about that. Ignore my comments.
0

Because $man is object of class if you want to print some it should be in class try this code :

class Man{
    var $arr=array("name"=>"me");
}
$key='name';
$man=new Man;
echo $man->arr[$key]; //output me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.