O'Reilly logo

Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required