6 Commodious PHP Functions for Beginners

PHP comes up with more than 5000 major functions, checkout at PHP quick reference page. Discussing all of the functions and explaining them may found not possible here but we have selected handy part that are important to be considered. You might find them less trick but things that one should know because they can be widely used in different areas because of their versatility. There are a lot of tips and tricks that being developer you should look at, few of them are covered below.

In previous articles, we have touched the topics like Handy PHP and MySQL Queries for Developers and 22 Best PHP Tutorials For Developers.

php

 

1- The Elusory === Operator.

PHP is powerful and smart language. It is also known as loosely typed language because of its better understanding of variables. I noticed on forums and from blog comments, that a lot of people are confused about the usage of == and ===. How they’re different and what are their usages?

PHP do understand the ‘431’ as string but it can also be used to do subtraction or in common math. It makes sense! But what creates problem for PHP is the number 0. Zero (0) number can mean both 0 or false which at the end becomes little tricky and complex. There we have === to make the job easy.

Let’s get into the details.

Like any other language == are used for comparing the variables but while comparing the number 0, PHP might consider it as false rather than the actual number 0. That is the best place to use ===. This will not only compares the value but also the type. Use this operator to make sure it’s comparing the actual 0 number.

Example usage

[code type=php]

$myVar = false;

// Will return true
if($myVar == 0) {}

// Will return false
if($myVar === 0) {}

[/code]

 

2- Use Gravatars in Your Application

This is kinda snippet that I thought may aid readers and is worth sharing. The immense popularity of WordPress also give hand to Gravatar usage. Gravatar provides smart API and made the usage easy to integrate.

[code type=php]

/*
-myEmail- Email address to show gravatar for
-gSize – size of gravatar
-url – URL of default gravatar to use
-gRating – rating of Gravatar(G, PG, R, X)
*/
function myGravatar($myEmail, $gSize, $url, $gRating)
{
echo ‘‘;
}

[/code]

Once you have added the above function, you may call it like below at any area where you want to show the Gravatar image.

[code type=php]

myGravatar($myEmail, $gSize, $url, $gRating);

[/code]

 

3- Empty() – Ternary Operators

One of the very handy PHP function, which is barely available for any other language is empty() function. A ternary operators work different from if statement. What this function actually do is to check the variable to see if that’s empty or have some value in it. It also check for null, false or number 0 (which are all considered as zero) and returns true or false.

[code type=php]

function PassValue($foo)
{
if(empty($myVar))
return false;
}

[/code]

or if we use the simple one example using if conditions, we’d have the one of the following:

[code type=php]

if(!empty($_GET[‘myVal’])){

$myVar = $_GET[‘myVal’];

}else{

$myVar = ‘SmashingHub’;

}

// Or something like below does the same job

$myVar = ‘SmashingHub’;

if(!empty($_GET[‘myVal’])){

$myVar = $_GET[‘myVal’];

}[/code]

php tips

 

4- Function_exists()

This function will check for it that specific function exist or not. It’s kinda conditional statement that will first verify and executes only if that specific function exist. While working in some CMS like WordPress, there are a lot of plugins and functions integrated. Let’s come to an example!

Very back when I was changing my blog theme, while switching I encountered a problem. There was some call to undefined function error displayed at sidebars. I looked into the source code and founded get_flickrrss() function being called, which requires a plugin installed to work.

What I’m trying to clear with above example, if the developer of theme have had used function_exists() to check to execute function only if that plugin function exists or installed.

Bad Usage:

[code type=php]

get_flickrrss();

[/code]

Recommended Usage:

[code type=php]if (function_exists(‘get_flickrrss’)) {

get_flickrrss();

}[/code]

 

5- Referencing

Referencing is the way to send variables to function without returning anything or setting any variable globally. This method will just alter the original variable. We use an ampersand (&) symbol in the start of function.

Example:

[code type=php]

function lowercase(&$myString){

$myString= strtoupper($myString);

}

$myName = ‘smashinghub’;

lowercase($myName);

echo $myName;  // returns SMASHINGHUB

[/code]

In above example, we called strtoupper method and pass through the $myName, which have altered the actual string value.

For sure there are benefits in going with referencing like returning anything is not important from the function and you don’t have to define them as for globally accessible.

 

6- Similar_text

Last but not the least, PHP have a similar_text function that will check for the two string for the similarity.

[code type=php]

similar_text($myVar1, $myVar2, $sPercent);
//$sPercent will return the percentage of similarity

[/code]

 

Closing Thoughts

PHP have a lot of learning margin, even we know very big part but overall that’s just a one dimension. It’s a way more extensive thing. Being a good developer, we always tend on learning and sharing useful stuff with fellows to help others and to sharp your very own skills.

What’s your favorite functions list? Do you have some smart snippets that you think should be shared to world? Let us know in below comments.


About the author

With a passion for Knowledge, Smashinghub has been created to explore things like Free Resources For Designers, Photographers, Web Developers and Inspiration.

Twitter Visit author website

Subscribe to Smashing Hub


1 Comment

  1. cody dmd says:

    great & very useful article !
    appreciate it ! 🙂