|
Posted by onembk on 09/04/06 02:12
On 2006-09-03 19:19:43 -0600, Adam <anon@nowhere.com> said:
> On Sun, 03 Sep 2006 18:06:50 -0400, Koncept wrote:
>
>> In article <4lvv78F3qicuU1@individual.net>, J.O. Aho <user@example.net>
>> wrote:
>>
>>> kenoli wrote:
>>>> I often see syntax used in code snippets that is different than that
>>>> indicated in the php manual, at least in the main topic pages.
>>>
>>> How do you mean differs?
>>>
>>> like:
>>>
>>> $str1 = substr('abcdef', 1);
>>> $str2 = substr('abcdef', 0, 8);
>>>
>>> or?
>>>
>>>
>>>> Is there somewhere that I can find alternative syntax documented?
>>>
>>> The online manual gives you the valid syntaxes, or course they don't
>>> give all the examples how you can use functions.
>>>
>>>
>>>> I suspect that in some cases this may be a matter of the author using C
>>>> syntax that is slightly different than php syntax.
>>>
>>> PHP don't understand any other syntax than PHP.
>>>
>>>
>>> //Aho
>>
>> He probably means:
>>
>> [quote]
>> Alternative syntax for control structures
>>
>> PHP offers an alternative syntax for some of its control structures;
>> namely, if, while, for, foreach, and switch. In each case, the basic
>> form of the alternate syntax is to change the opening brace to a colon
>> (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;,
>> or endswitch;, respectively.
>>
>> <?php if ($a == 5): ?>
>> A is equal to 5
>> <?php endif; ?>
>>
>> In the above example, the HTML block "A is equal to 5" is nested within
>> an if statement written in the alternative syntax. The HTML block would
>> be displayed only if $a is equal to 5.
>> The alternative syntax applies to else and elseif as well. The
>> following is an if structure with elseif and else in the alternative
>> format:
>>
>> <?php
>> if ($a == 5):
>> echo "a equals 5";
>> echo "...";
>> elseif ($a == 6):
>> echo "a equals 6";
>> echo "!!!";
>> else:
>> echo "a is neither 5 nor 6";
>> endif;
>> ?>
>>
>> [/quote]
>
> He may also mean the use of short quotes (<? over the more common
> <?php) which allows for some other variations, I think.
>
> Adam.
There's all kinds of alternative syntax in PHP. Things like:
if ($a == $b)
{
$c = $d;
}
is the same as
if ($a == $b)
$c = $d;
while
if ($a == $b)
{
$c = $d;
}
else
{
$c = $e;
}
is the same as
$c = ($a == $b) ? $d : $e;
Another one is
<p><?php echo $a; ?></p>
is the same as
<p><?=$a;?></p> (semi-colon optional)
That's all I can think of off the top of my head. I'd be interested to
know what other cool shortcuts there are and what php.ini settings they
require (that last one requires short tags I believe).
Navigation:
[Reply to this message]
|