Jump to content

Basic PHP & JS questions


apeisa
 Share

Recommended Posts

I am reading through source code and there is some syntax that is unfamiliar to me. First in templates there is line like these:

echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";

Why there is {} around properties? Does it have any function or does it only make code cleaner?

And then at ProcessPageList.js there is lines like this:

if(child.status & 1024) $li.addClass('PageListStatusHidden secondary'); 

What does & at that line? I have a good guess, but I have never seen that syntax before so better ask than assume :)

This probably isn't best forum for such a general programming questions, but it would really help me to understand what is happening under the hood (and of course learn some new programming at the same time).

Link to comment
Share on other sites

Hi Apeisa,

welcome to the forums, although google is your friend in this case, I'll be easy on you :)

{} brackets

Is so called Complex syntax. It allows you to use more complicated $values in parsed strings, consider following:

	echo "$arr['foo'][4]";

throws syntax error, unexpected T_ENCAPSED_AND_WHITESPACE

	echo "{$arr['foo'][4]}";

on the other hand, will work, because the curly brackets delimited parts of string, that should be considered as $variable.

More here: http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

P.S.: even simple parsing can parse objects ($object->property), but I think Ryan considers it best practice to use curly brackets (not to mention it also increases readability)

& operator

Amp in this case is so called Bitwise And.

You can read about it here: http://www.php.net/manual/en/language.operators.bitwise.php

In this case, it compares two int values – page.status and value 1024. is page.status has the '1024' bit set (= if page.status value is 1 in binary form on 210th place) it continues.

Link to comment
Share on other sites

Thank you Adam!

I tried Google (as always), but as non native English speaker I have sometimes difficulties to find right queries when it comes to special characters. Google tends to ignore single characters like { and &

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...