PHP Functions
PHP comes with hundreds of ready-made, built-in functions, making
it a very rich language. To use a function, call it by name. For
example, you can see the print
function in action here:
print("print is a function");
The parentheses tell PHP that you’re referring to a function. Otherwise, it thinks you’re referring to a constant. You may see a warning such as this:
Notice: Use of undefined constant fname - assumed 'fname'
followed by the text string fname
, under the assumption that you must have
wanted to put a literal string in your code. (Things are even more
confusing if there is actually a constant named fname
, in which case PHP uses its
value.)
Note
Strictly speaking, print
is a
pseudofunction, commonly called a construct. The
difference is that you can omit the parentheses, as follows:
print "print doesn't require parentheses";
You do have to put parentheses after any other function you call, even if they’re empty (that is, if you’re not passing any argument to the function).
Functions can take any number of arguments, including zero. For
example, phpinfo
, as shown here,
displays lots of information about the current installation of PHP and
requires no argument. The result of calling this function can be seen in
Figure 5-1.
phpinfo();
Note
The phpinfo
function is extremely useful for obtaining information about your ...
Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.