allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 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 More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 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. Link to comment https://forums.phpfreaks.com/topic/145645-construct-vs-functions/#findComment-764620 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Author Share Posted February 17, 2009 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... Link to comment https://forums.phpfreaks.com/topic/145645-construct-vs-functions/#findComment-764625 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 Constructs in general are for most basic actions. If you want to memorise them, good thing is their number is relatively small, compared to the number of functions. Link to comment https://forums.phpfreaks.com/topic/145645-construct-vs-functions/#findComment-764628 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Author Share Posted February 17, 2009 Constructs in general are for most basic actions. If you want to memorise them, good thing is their number is relatively small, compared to the number of functions. Below are language constructs right? __autoload(), __construct(), __destruct() Link to comment https://forums.phpfreaks.com/topic/145645-construct-vs-functions/#findComment-764629 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 I don't think so, but I am not sure. PHP manual refers to __autoload as to a function http://www.php.net/manual/en/language.oop5.autoload.php and constructor and destructor are methods, which makes them functions as well. Link to comment https://forums.phpfreaks.com/topic/145645-construct-vs-functions/#findComment-764633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.