Archive for the 'Built-in PHP functions' Category

Basic Language Functions

PHP includes a number of convenient built-in functions. These built-in functions are written in C, and are all faster than what you could accomplish with a custom function written in PHP.

The set of functions I want to introduce you to in this video all deal with your variables and what they are.

The first function is called ‘isset’. ‘isset’ will return true if you call it on a variable that has been assigned to a non-null value. Unlike most other PHP functions, isset is actually a language construct. For most purposes, there isn’t much distinction between it and the other built-in functions. However, it’s worth pointing out that you must invoke it on a variable, even one you haven’t yet declared. If you try invoking it on a string or an int or the return value of another function, your script will fail to execute, and even with error reporting enabled, you won’t see an error notice.

{show isset being called with a literal}

Generally mentioned in the same breath as ‘isset’ is the function ‘unset’. unset is also a language construct, and also cannot be called on anything other than a variable. All it does is clear a variable’s contents and remove it from memory. If you try accessing that variable again later, it’s value will report as null.

The last language construct in this set is the function ’empty’. In some sense you can think of it like the counterpart to isset. Again, it can only be called on a variable, but instead of telling you whether a variable is set, it tells you whether that variable evaluates to false. So, if you set a variable to either null, the empty string, the boolean false, an empty array, or the number 0 it will return ‘true’, because when PHP converts any of those to booleans they all evaluate to false. I know, the logic sounds a little backwards when I explain it that way, but that’s how it works. If you test a variable with any value that evaluates to true, ’empty’ returns false.

The next set of methods can tell you much more about what a variable actually is, and these are not language constructs, by the way. Whereas language constructs are the lowest-level elements of the PHP language, the rest of these functions (like most functions PHP provides) are built up from a combination of C code and the language constructs. As far as your everyday coding goes, if you use Zend Studio, as I do, it just means that they don’t get colored blue automatically when you type them. You could also invoke them on literal strings or ints, the return values of other functions or whatever, and not just actual variables, if you were so inclined.

Now, I’m going to go through these kind of fast, but one of the nice things about these functions is I can just introduce them by name and it’s obvious what they do. So, first we have is_array to test whether a variable is an array. is_object tests whether something is an object. is_resource checks for resources. We talk more about objects in a later chapter. You can use is_scalar to test for something that’s neither an array, a resource, nor an object.┬áis_numeric tests for numbers, whether they’re ints or floats. If you just want to test for a float, you can use is_float or is_double (they’re exactly the same functions). If you just want to test for ints, you can use is_int, is_integer, or is_long (all 3 are exactly the same function). is_string tests for strings. is_boolean tests for booleans. There’s even a function to test for nulls called ‘is_null’. Functionally, is_null is the exact opposite of isset, except that it won’t crash your script to call it on a literal or on the return value of another function.

At this point you’ve probably noticed a pattern. If you just start searching for functions starting with the prefix ‘is_’ you’re going to find a whole lot more just like the ones I’ve mentioned.

{bring up PHPfr showing all functions with the ‘is_’ prefix}

Every different variable type has an ‘is’ function associated with it. And there are very useful ‘is’ functions to test for things like whether or not a particular constant has been defined, whether a particular directory is writable, whether an object is an instance of a particular class or sub-class, whether the filepath you’re working with represents a file that’s been uploaded from a form. The list isn’t exactly endless, but it’s pretty darn long. All of these functions perform some sort of test on your variable and return true or false depending on the test and the variable. The ones I’ve mentioned are probably the ones you’ll use the most, but they all have their place once in a while.

If you don’t want to run a true-false test for the type of a variable, but just want the variable directly, use ‘gettype’. It has 8 possible return values:

{bring up PHPfr page for ‘gettype’}

array
int
double – we usually call these floats, but it’s returned as ‘double’ for historical reasons
string
resource
object
boolean
null

February 04 2010 | Basics and Built-in PHP functions and PHP tutorial scripts and Schmategories | No Comments »