Intro to PHP
PHP is a script on the server-side used for the creation of Static or Dynamic Web sites or Web applications. PHP is a pre-processor for hypertext, which used to stand for home pages.
What can PHP do?
With this, we can
- Generate dynamic web pages.
- Collect form data from the web page.
- Send or receive cookies.
- And anything which any other CGI (Common Gateway Interface) programming language can do.
There are three main fields where these scripts are used:
- Server Side Scripting – This is the traditional purpose of why it is built & used for generating dynamic content. For this, you need a PHP parser, a web server, and a web browser.
- Command-line Scripting – This is where PHP acts as just any other scripting language wherein you can run your PHP scripts using a PHP Parser.
- Client-side GUI – This is where you can build application software (with GUIs) using PHP-GTK. It is not the best-fit language for the purpose, though.
PHP is really cool.
Initially designed to perform little more than an accountant and a guestbook, PHP has experienced in a short time a real revolution and, based on its functions, can now perform a multitude of tasks useful for web development :
EMAIL FUNCTIONS
We can with astonishing ease send an e-mail to a person or list parametrizing a whole series of aspects such as the origin e-mail, subject, person to respond ...
Other less frequent but undoubtedly useful functions for managing emails are included in your library.
DATABASE MANAGEMENT
It is difficult to design a current, powerful and content-rich site that is not managed by a database.
The PHP language provides interfaces for access to most commercial and ODBC databases to all possible databases in Microsoft systems, from which we can edit the content of our site with absolute simplicity.
FILE MANAGEMENT
Create, delete, move, modify ... any kind of operation more or less reasonable that can occur can be done from a large library of functions for the management of files by PHP. Also we can transfer files by FTP from sentences in our code, protocol for which PHP has also provided a lot of functions.
IMAGE PROCESSING
Obviously it is much easier to use Photoshop for a treatment of images but ... And if we have to treat thousands of images sent by our netizens?
The truth is that it can be very tedious to uniformize in size and format thousands of images received day after day. All of this can also be effectively automated through PHP.
PHP Tricks :
PHP Exception Handler
When an error occurred PHP script displays a fatal error. To avoid the error you should need to write the proper code to handle the exception in PHP script. PHP exception handler is a smart way to handle the exceptional condition.
//trigger exception in try block and catch exception
try {
//something
} catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
Default Variable Value
Always assign a default value of a variable
$var = "default";
if (condition) {
$var = "something";
}
Return a result and error from a function.
Comes in real handy when creating validation functions.
list($result, $error) = validateString($str);
if($error){
// Do something.
}
function validateString($str){
return array(false, "String is invalid");
}
Ternary Operator
Ternary operator consistent with three expressions separated by a question mark (?) and a colon (:). It makes if/else logic shorter, quicker, and easier.
$name = !empty($_GET['name'])? $_GET['name'] : 'CodexWorld';
Convert XML to associative array
one-liner to convert XML into associative array.
<?php
$arr = json_decode(json_encode(simplexml_load_string($xml, null, LIBXML_NOCDATA)), true);
?>
Count characters in a string
<?php
$string = 'testing';
if(isset($string[6]))
echo "The string '$string' is at least 7 characters long.";
else
echo "The string '$string' is less than 7 characters long.";
PHP list() function
Use list() function to assign variables as an array.
$person = ['Random Name', 'name@example.com'];
list($name, $email) = $person;
Functional blocks
function redirectTo($route) {
header("location: $route", true, 302);
}
Use Helper
Use a helper to provide an easy access to user submitted data that may not be in the correct format.
$input->string('key', 'Default Value');
$input->int('anotherKey', 55);
Prevents SQL injection
There are many ways to avoid SQL injection but the simplest one escapes any variable that we will use in the database. The code should look like this:
$query_result = mysql_query ( "SELECT * FROM WHERE Ex_table ex_field = \" " . mysql_real_escape_string( $ ex_field ) . " \ " " ) ;