Ninjakreborn Posted January 18, 2011 Share Posted January 18, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/ Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 Alternative syntax Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161516 Share on other sites More sharing options...
Maq Posted January 18, 2011 Share Posted January 18, 2011 As Pika pointed out there's alternative syntax for IF statements. In this particular case an if, else, or elseif will execute what is started on the first line directly following it. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161520 Share on other sites More sharing options...
Psycho Posted January 18, 2011 Share Posted January 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161521 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161633 Share on other sites More sharing options...
BlueSkyIS Posted January 19, 2011 Share Posted January 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161639 Share on other sites More sharing options...
Coolkat Posted January 19, 2011 Share Posted January 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161680 Share on other sites More sharing options...
parino_esquilado Posted January 19, 2011 Share Posted January 19, 2011 Honestly this is the best: $c = ($a == $b) ? 1 : 0; haha Yes!! I love the ternary operator. Though it's an IF-ELSE statement essentially, so if you JUST want it to be an IF statement then I'd avoid using it. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161681 Share on other sites More sharing options...
Maq Posted January 19, 2011 Share Posted January 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1161921 Share on other sites More sharing options...
Psycho Posted January 19, 2011 Share Posted January 19, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1162136 Share on other sites More sharing options...
Ninjakreborn Posted February 9, 2011 Author Share Posted February 9, 2011 I appreciate the feedback. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/224877-what-is-this-syntax/#findComment-1171682 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.