Missing semicolon doesn't error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Missing semicolon doesn't error

Post by RobertGonzalez »

Ok, I thought I knew it all (ok most, at least) but while testing today I did this:

Code: Select all

<?php
phpinfo()
?>
and I got no errors. When I did this:

Code: Select all

<?php
phpinfo()
if ($yardley) {
    echo 'Whos da man';
}
?>
and this

Code: Select all

<?php
phpinfo();
if ($yardley) {
    echo 'Whos da man'
}
?>
I did get errors. How come that happens? I have never tried that before, but it seems odd that a missing semicolon would not throw an error. Or am I missing something.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Haven't you ever done this?

Code: Select all

<a href="<?php echo $page ?>">Some Place</a>
When there is just one statement on the page... semicolon can be omitted.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Read the *cough* *cough* ;)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Everah, I bet you didn't learn PHP from a book. Am I right?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The closing tag of a block of PHP code automatically implies a semicolon
I wasn't aware of that either. Nifty!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Oren wrote:Everah, I bet you didn't learn PHP from a book. Am I right?
You got it. I learned it from tutorials, this forum and good old trial and error...
The Ninja Space Goat wrote:Haven't you ever done this?

Code: Select all

<a href="<?php echo $page ?>">Some Place</a>
When there is just one statement on the page... semicolon can be omitted.
... but I have never made this 'error'. The way I learned PHP was to always terminate your lines. Good thing is you are never too advanced to learn the language (unless you are feyd :) ).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I always add the semicolon even when it's not needed - so if I add new lines I don't need to add it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I have always been of the mind set that if you open something in the code, you close, even if it is not necessary (like ending PHP tags, <db>_close(), etc). It just seems right.
Post Reply