Check Your Internet Speed

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Best Five PHP Tutorials For Developers

PHP is the most widely-used language for programming on the web. If you’re new to PHP, then it’s time to get acquainted with the awesomeness that is the PHP manualPHP helps creates dynamic web pages with load of functionality. You can improve your PHP with following tips:
You can turn on Error Reporting in PHP that you might not have spotted earlier.
You can use IDE’s (Integrated Development Environments) Tools for any Development. It can help you while syntax highlighting, Code Completion, Error Warnings and Refactoring (reworking.
So, in this round up you will find some useful and PHP tutorials that are especially designed for latest technics for developers. We hope that you will get good help from these tutorials. Enjoy!

Create Instagram Filters With PHP

Create Instagram Filters With PHP

Hand Made PHP and MySQL Queries for Developers

MySQL was initially introduced in back 1995 based on C and C++ programming languages. It is the most used open source database today because of the great verstality in online applications. MySQL is claimed over 65,000 downloads per day. It is also the most preferred choice of  new generation and is best used with PHP / Perl / Python. Millions of websites and companies like Google, Facebook, Alcatel Lucent and Zappos are relying on MySQL. It runs on almost all major platforms including Mac OS, Windows, Linux and Solaris.
MySQL is loved by a broad number of database developers, DBAs and IT managers. The reason behind is the high performance database that is reliable, affordable, and easy to use. Whether you’re a new user or an experienced developer, you’ll prefer MySQL because of its wide development tools, training and support services that are key factor for any successful development.
In today’s article, we’re covering clever ways of tricking MySQL and PHP to perform your work smartly.

1- Cloning a database or table using MySQL queries.

Below are the simplest way to clone any database table using the query which helped me a lot in my developments. Categorizing the queries into two sets will ease understanding them.

a- Clone Table Structure and Keys Only

Firstly we’re assuming that your script needs to create a fresh table for user with structure similar to the existing user tables. This is the situation where you just need to duplicate a structure and keys of some table but not the data, this can be achieved with below query.
1
2
CREATE TABLE newTableName
LIKE oldTableName
The same above query works for databases as well but you must need the permissions to do so.

b- Clone Database Table with all data

In case you need to clone the whole table along with the data, below methods can help you.
1
2
3
CREATE TABLE newTableName
SELECT *
FROM oldTableName
The same can be achieved with the help of two queries like below:
1
2
3
4
5
6
CREATE TABLE newTableName
LIKE oldTableName;

INSERT INTO newTableName
SELECT *
FROM oldTableName;
For newbies, complete syntax for Create Table is here.

2- Return One Variable from MySQL Query

PHP is the leading language today that is preferably used with MySQL databases. We can make some smart functions to ease our work. This query is tech savvy and will help you get the one variable value using a simple PHP function.
1
2
3
4
5
6
7
function quickVarGet($myquery){
$execQuery = mysql_query($myquery);
$myRow = mysql_fetch_array($execQuery);
mysql_free_result($execQuery );
$output = $myRow[0];
return $output;
}
So you’re done. Use the below command anywhere to return the one variable.
1
$email = quickVarGet("SELECT email from users where id = 1");
The above query will return the email address only, the output may be “admin@smashinghub.com” considering there was a record in the database.

3- Get the ID of last MySQL Last Entry

Sometimes we need to get the ID of last entry that some query just made. In order to achieve this thing below functions can help you.
1
2
3
//Make sure database is connected
$getTheLastID = mysql_insert_id();
echo $getTheLastID;
This function will return the ID of last MySQL query but this functions seem to work on queries that previously run in the same DB connection.
Anyway, let us know was it useful?

4- Making your Script MySQL Injection proof

Web is much save these days but still a lot of new developers don’t bother to make their scripts injection proof. Using below simple PHP function will make it impossible for injectors to hack into your database.
1
mysql_real_escape_string(trim($var));
This can be done more smartly by creating some function to make it easier to use on different locations.
1
2
3
4
5
function protect($var)
{
$var= mysql_real_escape_string(trim($var));
return $var;
}

Summing Up

Developers memorize a lot of queries in their mind, some are very complex while some are simpler. For every successful developer, its important to have some tech savvy smart queries in mind to trick the code easily. This not only will enhance the speed and workflow but also will ease your development with greater powers.

Source: Smashinghub.com

Cool Six Commodious PHP Functions for Beginners Language

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.

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
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 isempty() 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:
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'];

}

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.

Websites Resources | Blogging | Technology News | Softwares - i Developments