Jump to content

What is this syntax?


Ninjakreborn

Recommended Posts

<?php
/* Register footer widget */
if (function_exists('register_footercounter') )
register_footercounter(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
?>

How exactly does this syntax work.  I am not use to seeing this. It is an if statement without brackets.  It uses a function inside of it with an array.  So, I do not exactly understand what is happening here.  I have used php for a long time and just now started working with Wordpress awhile back, and reviewing how to do themes, and I am seeing this kind of

funky syntax all over the place.  Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/
Share on other sites

To add to Maq's explanation: The six lines of code following the IF statement is interpreted as a single line of code to the PHP interpreter. It is a single function call with one parameter as an array that is defined over multiple lines. The semi-colon at the end is what tells the parser that the line is done - not the line breaks.

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161521
Share on other sites

I think it's better if you use braces always tbh.

 

<?php

if($a == $b)
    $c = 1;
$d = 4;
$e = $d - 6;

should be written as (in my opinion)

if($a == $b) {
    $c = 1;
}
$d = 4;
$e = $d - 6;

?>

 

It is more clear where the if statement ends in the second example.

 

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161633
Share on other sites

This syntax works for more than just if else, it also works for loops. such as:

 

for($i = 0; $i < $j; $i++)
   some_func($i);

 

is the same as

 

for($i = 0; $i < $j; $i++)
{
   some_func($i);
}

 

I personally like the lack of a bracket for simple commands like that, but more complex functions with lots of parameters (or an array like stated before) I would use brackets.

 

Honestly this is the best:

$c = ($a == $b) ? 1 : 0;

haha

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161680
Share on other sites

i agree with parino_esquilado. imo, it's one of those 'just always do it for consistency even though you don't have to always do it' things.

 

Agreed 100%.

 

This syntax works for more than just if else, it also works for loops

 

Good point.

 

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161921
Share on other sites

Honestly this is the best:

$c = ($a == $b) ? 1 : 0;

 

It depends. Since you are setting the value to 1/0 are you wanting the value of $c to be a boolean value or are you going to use the numerical values of 0 and 1 in calculaions? If you are only wanting to set $c to a boolean then it is unnecessary to create a condition such as "if this condition is true set this value to true otherwise set this value to false". Instead, just create a line such as "set this value to this condition", such as:

$c = ($a == $b);

Link to comment
https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1162136
Share on other sites

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.