6 Commodious PHP Functions for Beginners

here Binary Option Mint

http://agatecloud.com/?p=Laureate-International-Universities-Stock-Exchange Laureate International Universities Stock Exchange

Realities At The Currency Exchange Forex Rates Calculator

here Binary Options Signals Provide

1- The Elusory === Operator.

Online Trading Institute

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

1
2
3
4
5
6
7
$myVar = false;

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

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

 

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.

1
2
3
4
5
6
7
8
9
10
/*
-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 '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($myEmail). '&amp;default='.$url.'&amp;size='.$gSize.'&amp;rating='.$gRating.'" alt="" width="'.$gSize.'px" height="'.$gSize.'px" />';
}

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

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

 

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.

1
2
3
4
5
function PassValue($foo)
{
if(empty($myVar))
return false;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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'];

}
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:

1
get_flickrrss();

Recommended Usage:

1
2
3
4
5
if (function_exists('get_flickrrss')) {

get_flickrrss();

}

 

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:

1
2
3
4
5
6
7
8
9
10
11
function lowercase(&amp;$myString){

$myString= strtoupper($myString);

}

$myName = 'smashinghub';

lowercase($myName);

echo $myName;  // returns SMASHINGHUB

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.

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

 

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 ! :)