How does PHP read the PHP whole script / file?

General discussions related to php

Moderators: macek, egami, gesf

Post Reply
User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 286
Joined: Sat Mar 23, 2013 8:24 am

Re: How does PHP read the PHP whole script / file?

Post by Strider64 » Sat Apr 13, 2013 11:10 am

To answer your question the function can go anywhere (though most programmers put functions at the top), so that tells you what you are doing with that script. ;-)

echo is just outputting the variable, the script would still run correctly without it.

and test1($var1); is just calling the function....

The only thing that would make it throw a tizzy is if you delete $var1 = "1st var"; for then you would have an undefine var.

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 286
Joined: Sat Mar 23, 2013 8:24 am

Re: How does PHP read the PHP whole script / file?

Post by Strider64 » Sat Apr 13, 2013 5:36 pm

Since PHP is a scripting language every line of code is evaluated, that is if I'm understanding you question. The only time it doesn't from a programmers point of view is if it is in a function.

for example

Code: Select all

function first() {
   $var1 = "Hello, World!";
   echo $var1;
}
if it is never called it will never run; however, I'm sure some memory (relatively small compared to today's processing power :D ) is put aside for the function in case it is ever called. I just like to add, that I'm sure you won't run into that problem, for once you get the hang of writing loops, conditions, functions, classes, etc.. you find yourself writing less code.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: How does PHP read the PHP whole script / file?

Post by seandisanti » Mon Apr 15, 2013 7:29 pm

at run time, the entire script including functions and includes are evaluated, even if not executed. For proof, just run a script that contains an error in an orphan (uncalled) block like this:

Code: Select all

<?php
function blah(){
    x
}
echo 'error ignored';
 
You'll receive a parse error instead of the echo'd 'error ignored'

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests