Cover by Robin Nixon

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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 it’s 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 below, 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();
The output of PHP’s built-in phpinfo function

Figure 5-1. The output of PHP’s built-in phpinfo function

Note

The phpinfo function is extremely useful for obtaining information about your current ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required