Home > PHP/MySQL > PHP Concepts: The Difference Between Functions & Language Constructs

PHP Concepts: The Difference Between Functions & Language Constructs

by Sam on October 26, 2009 · 5 comments

in PHP/MySQL

Persons new to programming often have difficulty grasping the concept of language constructs. Furthermore, both new programmers and seasoned programmers often get tripped up when it comes to telling the difference between language constructs and functions as they often resemble each other and in many instances are used in the same way. As a result, many developers write code for a number of years mistaking language constructs for functions and only become aware of the difference after trying to fix some odd quirk in their applications which arises because of a misunderstanding of the fundamental differences between the two.

Functions

Essentially, a function is a block of code which is written in such a way that it may be used and reused multiple times in the execution of a script. It may be designed to accept arguments and return values or it may do neither. If it is designed to take arguments, they will invariably affect the procedures of the function and, quite likely, any value(s) returned by the function. In PHP, functions may be compiled with the language (referred to as core or native functions), they may be user defined (i.e. created by the programmer writing the script) or they may be accessed as components of external libraries or extensions available via PECL. Examples of PHP functions include:

  • json_encode()
  • mysql_connect()
  • preg_replace()
  • strtolower()
  • array_shift()
  • unlink()
  • mail()
  • ob_start()
  • file_get_contents()
  • curl_init()

Language Constructs

Language constructs are keywords that are a part of the syntax of the language, i.e. they are parts of the language itself. They cannot be user defined nor can they added to the language via extensions or libraries. They may or may not take arguments and they may or may not have return values (although most of them don’t). Examples of language constructs in PHP include:

  • echo()
  • print()
  • die()
  • include()
  • require()
  • empty()
  • isset()
  • unset()
  • list()
  • array()

Note that some language constructs do not require the use of parentheses:

print('one');
//works in the same way as
print 'one';
//the above code outputs 'oneone' and
include('file.php');
//works in the same way as
include 'file.php';

The Difference

The key difference between functions and language constructs is that language constructs are the most basic units of the language and cannot be broken down further by the PHP parser whereas functions have to be further broken down before being parsed, ofetn into language constructs. In other words, in just the same way that PHP code has to be broken down into lower level opcode by the PHP parser in order for the machine to understand it, functions must be broken down to language constructs by the PHP parser before they are parsed. There are a few interesting consequences of this:

  1. Language constructs tend to be faster than their function counterparts.
  2. Language constructs in some cases may be able to bypass error handling mechanisms.
  3. While functions can be disabled in PHP via the configuration file, language constructs cannot.
  4. Language constructs cannot be used as callback functions.

Happy coding!

Short URL for this post:

http://bit.ly/3zeJbn

Comments Map

Location data courtesy of GeoSmart

{ 3 trackbacks }

United States Webby Scripts PHP Concepts: The Difference Between Functions & Language Constructs from California, United States
October 26, 2009 at 4:22 am
United Kingdom PHP Concepts: The Difference Between Functions & Language Constructs from United Kingdom
October 26, 2009 at 6:47 am
United States Tweets that mention PHP Concepts: The Difference Between Functions & Language Constructs -- Topsy.com from California, United States
October 26, 2009 at 9:31 am

{ 2 comments… read them below or add one }

1 United States Richard Lynch from Illinois, United States October 27, 2009 at 9:27 pm

Twitter: @LynchRichard

Excellent article.

You may want to do version 2 that explains some of the most common ways things go wrong when programmers make the mistake of pretending a language construct is a function.

2 India Jai from Delhi, India October 28, 2009 at 1:09 am

Excellent article !

I think , this is the way to differentiate both the things. And this is the greatness of Open Source that always we are helping each other to grow and know more about the technology.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: