+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: A VB-like Syntax

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    A VB-like Syntax

    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:

    Code:
    <?php

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

    ?>
    However, you can also do it like this:

    Code:
    <?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:

    Code:
    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:

    Code:
    <?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!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: A VB-like Syntax

    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.

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: A VB-like Syntax

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

    BTW, did you +rep?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: A VB-like Syntax

    Or you can refer to my blog: PHP Colon Syntax

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: A VB-like Syntax

    Woah, didn't see that - but my one's better, I think.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Jordan Guest

    Re: A VB-like Syntax

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

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: A VB-like Syntax

    Wa wa WHAT? Why not?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: A VB-like Syntax

    C'mon it's not that big of a deal.

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: A VB-like Syntax

    Yes it IS!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  11. #10
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: A VB-like Syntax

    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.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SQL syntax in PHP
    By Blue Indian in forum PHP Development
    Replies: 8
    Last Post: 09-29-2010, 07:40 AM
  2. Need help with syntax?
    By hayschooler in forum PHP Development
    Replies: 1
    Last Post: 04-13-2010, 04:51 PM
  3. help me [i have in syntax]
    By kiddies in forum C and C++
    Replies: 12
    Last Post: 06-15-2009, 08:35 AM
  4. Syntax
    By restin84 in forum C and C++
    Replies: 4
    Last Post: 03-27-2009, 06:38 AM
  5. Syntax
    By Sionofdarkness in forum C and C++
    Replies: 7
    Last Post: 07-23-2006, 08:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts