PHP syntax query

This is a discussion on "PHP syntax query" within the PHP Forum section. This forum, and the thread "PHP syntax query are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 7th, 2007, 11:40
Up'n'Coming Member
Join Date: Jul 2007
Location: France
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Question PHP syntax query

Quote:
<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>

<?php
}
else {

/* if the "submit" variable exists, the form has been submitted - look for and process form data */
// display result

$age = $_POST['age'];
if (
$age >= 21) {
echo
'Come on in, we have alcohol and music awaiting you!';
}
else {
echo
"You're too young for this club, come back when you're a little older";
}
}

?>
I'm slowly getting my head round php (not dissimilar from QBasic in a lot of ways...), but a couple of things in the above script weren't explained in a tutorial:

1. Why does the initial IF statement start with IF, to be followed by an opening brace, and then that piece of script closed? It's like saying, If so-and-so... forget it.

2. Then the third piece of script starts with a closing brace.

3. Therefore, why does an opening and closing brace overlap the end of one script and beginning of another?

Any helpful explanation appreciated.

Cheers
Reply With Quote

  #2 (permalink)  
Old Jul 7th, 2007, 15:22
Reputable Member
Join Date: Dec 2005
Location: U.S.A.
Posts: 147
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to ScottR Send a message via Skype™ to ScottR
Re: PHP syntax query

I'm not sure you can close a PHP block in the middle of am if else statment. I would have to look into that and try it. I would echo the entire form and escape the quotation marks where needed.

The second if else statement is a nested selection structure. Meaning that if submit is never set, the script never gets the else part of the selection structure, therefore never getting to the nested if else statement.

I don't see where anything starts with a closing brace? If you mean the closing brace under "Come on in, we have alcohol and music awaiting you". That is the closing brace for the if age >= 21 part of the script. Then goes to the else part where the user is not 21 of over.

Hope this helps,
Scott
Reply With Quote
  #3 (permalink)  
Old Jul 7th, 2007, 15:28
Up'n'Coming Member
Join Date: Jul 2007
Location: France
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP syntax query

Ending with an opening brace, lines 3 and 4:
{
?>

Lines 11 and 12, starting with a closing brace:
<?php>
}

It's in an online tutorial, and the script runs OK. I've seen it elsewhere as well.

In fact it's here under Part 4: http://www.goodphptutorials.com/track/229

Last edited by matelot; Jul 7th, 2007 at 15:31.
Reply With Quote
  #4 (permalink)  
Old Jul 7th, 2007, 15:56
Reputable Member
Join Date: Dec 2005
Location: U.S.A.
Posts: 147
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to ScottR Send a message via Skype™ to ScottR
Re: PHP syntax query

Ahhhh Ok I see what you mean! That is because the PHP code is interrupted by HTML. So the PHP block has to be closed and reopened. That was why I had mentioned echoing the entire form and escaping the quotation marks. That seems to me to be more of a logical approach (but that's just my opinion).

If the script works, I guess you can interrupt a a selection structure and reopen it.

Not sure if you know what I mean by escaping, but here a quick example though it is not tested.

Code: Select all
<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {
echo "
<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">
Enter your age: <input name=\"age\" size=\"2\">
<input type=\"submit\" name=\"submit\" value=\"Go\">
</form>"
}
The \ is the escape character and makes PHP ingnore whatever is after it. This way PHP won't recognize what's wrapped in quotation marks as a string.

Hope this helps,
Scott
Reply With Quote
  #5 (permalink)  
Old Jul 7th, 2007, 17:06
Up'n'Coming Member
Join Date: Jul 2007
Location: France
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP syntax query

Quote:
Ahhhh Ok I see what you mean! That is because the PHP code is interrupted by HTML. So the PHP block has to be closed and reopened...
This is the bit I was trying to understand because it seems at variance with other forms of programming e.g. closing a block with an opening brace and then opening a block with a closing brace. Seemed a bit bizarre!

Many thanks so far.
Reply With Quote
  #6 (permalink)  
Old Jul 7th, 2007, 17:42
Reputable Member
Join Date: Dec 2005
Location: U.S.A.
Posts: 147
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to ScottR Send a message via Skype™ to ScottR
Re: PHP syntax query

Your welcome. Hope it works out for ya.

Scott
Reply With Quote
Reply

Tags
curly braces, php, syntax

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
scene syntax Rick Flash & Multimedia Forum 5 Sep 27th, 2007 21:25
Help with syntax tox0tes PHP Forum 3 Sep 3rd, 2007 03:00
Valid php syntax nate2099 PHP Forum 2 Jul 18th, 2007 04:23
MySQL query query dangergeek Databases 3 Apr 12th, 2007 12:45
error in SQL syntax Drgreenfingers Databases 10 Jan 30th, 2006 02:00


All times are GMT. The time now is 05:40.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43