Jump to content

construct VS functions


allworknoplay

Recommended Posts

Ok, I am reading the 63  best practices to optimize PHP and it says that isset() is faster than using strlen() because it is a language construct instead of function like so:

 

if (strlen($foo) < 5) { echo "Foo is too short"; } 

 

if (!isset($foo{5})) { echo "Foo is too short"; } 

 

And here is what the article says:

 

Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length.

 

 

So this seems to be a little gray area for me. How can you tell what is language construct and what is a function? And what is really the difference? (I'm basically asking for more details on the difference that what was stated above...)

 

 

Link to comment
https://forums.phpfreaks.com/topic/145645-construct-vs-functions/
Share on other sites

Language Constructs

Constructs are elements that are built-into the language and, therefore, follow special

rules. Perhaps the most common of them is the echo statement, which allows

you to write data to the script’s output:

echo 10; // will output 10

It’s important to understand that echo is not a function and, as such, it does not have

a return value. If you need to output data through a function, you can use print()

instead:

echo 10;
print (10);

Another very important construct is die(), which is itself an alias of exit(). It allows

you to terminate the script’s output and either output a string or return a numeric

status to the process that called the script.

Language Constructs

Constructs are elements that are built-into the language and, therefore, follow special

rules. Perhaps the most common of them is the echo statement, which allows

you to write data to the script’s output:

echo 10; // will output 10

It’s important to understand that echo is not a function and, as such, it does not have

a return value. If you need to output data through a function, you can use print()

instead:

echo 10;
print (10);

Another very important construct is die(), which is itself an alias of exit(). It allows

you to terminate the script’s output and either output a string or return a numeric

status to the process that called the script.

 

Ok so basically, other than understanding the fundamental difference between language construct and functions, it's really nothing more than just taking the time to learn which ones are which?

 

I can probably bet a lot of people would have said that isset() is just a function without knowing that it is a built in language construct...

 

I guess I'll just have to accept which is what and try to learn them along the way...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.