Open In App

What is the difference between a language construct and a “built-in” function in PHP ?

In programming, language constructs and built-in functions are often misinterpreted with one another due to the fact that both have more or less alike behavior. But they differ from each other in the way the PHP interpreter interprets them. Every programming language consists of tokens and structures which the respective language parser can recognize. So whenever a file is parsed, the parser understands their usage and knows well what to do with them without having the need to examine them further. These tokens and structures are known as language construct. They are basically keywords that are a part of the programming language. In other words, they form the syntax of the language.
Following are some examples of language constructs:

echo()
include()
require()
print()
isset()
die()

Language constructs cannot be added to the PHP framework through any plugins or libraries. They may or may not return any values although most of them don’t. Also, some of them do not need the use of parenthesis.



Below examples illustrate the use of language construct in PHP:
Example 1:




<?php
  
 print('Monday ');
 print 'Tuesday ';
 $str = 'Wednesday';
 echo $str;
  
?>

Output:
Monday Tuesday Wednesday

Example 2:




<?php
/* PHP program to use unset
function */
  
$arr = array(
    "1" => "Amit",
    "2" => "Rajeev",
    "3" => "Mohit",
    "4" => "Manoj"
);
  
// Use unset function to
// unset element
unset($arr["2"]);
  
// Display array element
print_r($arr);
  
?>

Output:

Array
(
    [1] => Amit
    [3] => Mohit
    [4] => Manoj
)

On the other hand, built-in functions are blocks of code that are jotted down in such a way that they can be reused again and again in executing a specific task. They are already present in the PHP installation package. It is due to these built-in functions that PHP is an efficient scripting language.
Some common built-in functions used in PHP are:

json_encode()
mail()
explode()
rand()
curl_init()

Built-in functions are comparatively slower than their language construct counterparts. They have better code organization. They usually take input arguments and always return a value. Built-in functions usually comprise of date, numeric and string functions.

Below examples illustrate the use of built-in function in PHP:
Example 1:




<?php
/* Date functions in PHP */
  
 echo" Date and time is - ";
 print date("j F Y, g.i.a", time());
  
?>

Output:
Date and time is - 26 February 2019, 12.22.pm

Example 2:




<?php
/* String functions in PHP */
  
 $MyStr = "GeeksForGeeks";
 echo substr("GeeksForGeeks", 5, 3)."\n";
 echo trim("   GeeksForGeeks    ")."\n"
 echo str_replace("Geeks", "Code", "GeeksForGeeks");
  
?>

Output:
For
GeeksForGeeks
CodeForCode

Article Tags :