Learn PHP: Handy Functions for Beginners

Learn PHP: Handy Functions for Beginners

PHP Functions for Beginners

by Janeth Kent Date: 11-04-2013

If you’re new to PHP language, we’ll introduce you to seven really handy PHP functions in this quick tip!

Function 1 : isset

Check if a variable has been set. You just pass it a variable name, and it will return true if that variable exists, and is set to something other than NULL.

  1. $name = "Joe";   
  2. isset($name); // true  
  3. isset($age); // false  

This function also works with the items in array and associative arrays, so is often used to check for the existence of specific keys on the $_GET and $_POST superglobal arrays: if a given value exists, you’ll do one thing; otherwise, you’ll do something else. For example, a search page might go something like this:


  1. if(isset($_GET['query'])) {   You could also pass a step parameter to specify the increment between numbers:
  2.     // get results and display them  
  3. } else {  
  4.     // show some default content  
  5. }  

Function 2 : range

If you need a list of numbers to iterate over, you can use range function. Just pass it a starting and ending number (or letter), and it will return an array of the numbers:

  1. range(0, 10); // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)  
  2. range('a', 'f'); // array('a', 'b', 'c', 'd', 'e'. 'f');  

You could also pass a step parameter to specify the increment between numbers:

range(2, 10, 2); // array(2, 4, 6, 8, 10);  

Function 3 : list

This one’s pretty cool: let’s say you have an array, and you want to assign its items to variables of their own. The list function makes this super-simple:

  1. $array = ["Ellery", "Queen"];  
  2. list($first_name, $last_name) = $array;  
  3.   
  4. echo $first_name; // Ellery  
  5. echo $last_name; // Queen  

Here’s a good example from the PHP docs (for explode)

  1. $data= "foo:*:1023:1000::/home/foo:/bin/sh";  
  1. list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);  

Function 4 : basename

If you need to display information about a file you are working on, (show users the file name, and not its whole path), you can use basename. This function will strip that path down to just the file name; just pass it the path as the parameter; if you want to get rid of a suffix, like a file extension, pass that suffix as the second parameter.

  1. $path = "/some/long/path/to/the/special_file.txt";  
  2. $filename1 = basename($path); // special_file.txt  
  3. $filename2 = basename($path, ".txt"); // special_file  

Function 5 : strftime

The strftime function can format that timestamp in any way you’d like. You’ll pass it a format string and the timestamp and get the date back out.

strftime("%B %d, %Y", time()); // July 28, 2012

Of course, it’s impossible to memorize all the formatting tokens, so I use the handy strfti.me to help me; give it a try, and you’ll love it, too. 

Function 6 : strip_tags

If you want to strip out the HTML tags they enter, using strip_tags: 

  1. $message = "<div> This is my bio </div>";  
  2. echo strip_tags($message); // "This is my bio"

Of course, you might want to allow certain tags, like <strong>, <em>, or <code>, for some simple styling; pass a string listing those as the second parameter:

  1. $message = "<div> This is <strong>my</strong> bio </div>";  
  2. echo strip_tags($message, "<strong><em><code>"); // "This is <strong>my</strong> bio"   

Function 7 : array_rand

Let’s start with a simple one. Ever want to get a random item out of an array? You might use rand ormt_rand to get a random number, passing 0 and the last index of the array as the min and max parameters; this will give you a random key that you can use to pull a value from your array.

However, there’s a way that’s a little bit quicker: array_rand. Just pass it your array, and it will return the random key.

  1. $sites = ["Nettuts+", "Psdtuts+", "Mobiletuts+", "Mactuts+"];  
  2. $k = array_rand($sites);  
  3. $sites[$k];  
 
by Janeth Kent Date: 11-04-2013 hits : 2825  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 
Clicky