Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A VB-like Syntax

syntax

  • Please log in to reply
14 replies to this topic

#1 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 07 September 2008 - 12:13 PM

Hi, it's Xav. Now here's something pointless, but interesting nevertheless. It is actually possible to use another syntax when writing control structures in PHP! This could potentially be useful for VB programmers who want an easy way into PHP, before they learn the {} syntax.

Example 1

So, here's a normal If statement:

<?php

if ($forum == "CodeCall")
{
echo "This is the best forum!";
}

?>


However, you can also do it like this:


<?php if ($forum == "CodeCall"): ?>

This is the best forum!

<?php endif; ?>


The text inbetween the two PHP tags will only be printed if the variable $forum contains the value "CodeCall". This makes the two above statements identical.

Example 2

Here's another normal If statement, then:

if ($forum == "CodeCall")
{
echo "This is the best forum!";
}
else
{
echo "This is not the best forum!";
}


But we can rewrite it using a VB-like syntax, like this:

<?php

if ($forum == "CodeCall"):

echo "This is the best forum!";

elseif ($forum == "DiC"):

echo "This is a load of rubbish!";

else:

echo "I don't know this forum!";

endif;

?>


So, what's happened here:


  • The curly braces { } have disappeared from the code.
  • A colon : has been added after every condition statement.
  • We now have an "endif" statement to mark the end of the decision. This is followed by a semi-colon ;.

Conclusion

It may not be as nice as the { } syntax, but, like all other things in programming, has a use. Have fun!

Test yourself: Write a condition that rewrites the example above using a switch statement, but using the alternative syntax. Refer to PHP: Alternative syntax for control structures - Manual if you are stuck.

~Xav

+rep if useful. Leave any comments below!
  • 1
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 07 September 2008 - 12:39 PM

The only thing with not using the { } is that the code is a lot harder to read. If VB programmers are gonna learn PHP, they might as well learn it with the correct PHP syntax. :)
  • 0

#3 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 September 2008 - 08:00 AM

I thought it would be useful for people to know anyway.

BTW, did you +rep? :D
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 08 September 2008 - 12:13 PM

Or you can refer to my blog: PHP Colon Syntax
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 September 2008 - 12:15 PM

Woah, didn't see that - but my one's better, I think. ;)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 September 2008 - 12:19 PM

Nice tutorial. If you want points you must PM me. I also wont be giving out +rep during the contest for tutorials.
  • 0

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 September 2008 - 12:20 PM

Wa wa WHAT? Why not?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 08 September 2008 - 12:29 PM

C'mon it's not that big of a deal. :eek:
  • 0

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 September 2008 - 12:30 PM

Yes it IS! :D
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 08 September 2008 - 12:32 PM

I reserve +reputation for posts I find useful. That said, if you ever post something I find useful, you will receive reputation from me (as probably Jordan too). However, since tutorials are awarded 25 points toward winning the competition, Jordan sees no reason to award you with another 20 points for the same thing.
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 08 September 2008 - 01:09 PM

Aw, c'mon John, be the nice guy you are!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 adserverexpert

adserverexpert

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 21 September 2008 - 11:11 PM

its simple one.
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download