Showing posts with label What's new in PHP 7. Show all posts

Overview of PHP 7

1. What are the new features in PHP 7?

While migrating from PHP 5.6 to PHP 7, all PHP developers are so excited to know what are the new features in PHP 7. Well, PHP 7 does not disappoint anyone, here are some of the interesting new features applied in the new version.  


High performance & Consistent 64-bit support

This is the best feature that PHP 7 provides. While comparing to PHP 5.6 and HHVM 3.7, PHP7 is absolutely very fast in performance. It also provides high performance while using with frameworks and Content Management Systems(CMS).

Scalar type declaration

Scalar type declaration allows us to declare the function parameters and return the values with required data type. The return type can be any one from integer(int), string(string), floating point number(float) and boolean(bool) types.  

Return type declaration

Return type declaration specifies the data type of return value from a function, it is similar to argument type declaration.  

Null Coalesce operator

Null Coalesce operator ?? combines isset() and assignment of value, first it checks whether the variable exists or not, if it does then it takes the value, if not variable will be assigned with the second operand value. It is an interesting operator which decreases the time and long codes. 
$email =  $_GET['email' ??  'no email';



Spaceship operator

Spaceship operator <==> is used to compare two variables, it returns three values, 
-1 (less than)
 0 (equal to)
 1 (greater than)
It works with integer, floating point number and string also. 



<?php
echo <=> 1// 0
echo <=> 2// -1
echo <=> 1// 1
?>

Constant array

Constant array can be defined using define().

<?php
define
('Names', [
    
'Peter',
    
'Harry',
    
'Mary']);

echo 
Names[1]; // outputs "Harry"?>

Anonymous class

The concept of anonymous class is declaring a class within another class. For detailed information about anonymous class just visit here.

2. Why not PHP 6?

It's not like what we all think about, PHP 6 already existed and it was completely different from the new release. PHP 6 is kind of failure as it takes lot of CPU time and memory since it uses UTF-16 as default encoding.

Version 6 is failure, like PHP 6 is failure; Perl 6 is failure; MySQL 6 existed but have not been released, so this might have been a consideration.

PHP 6 project and it's features were widely known, and the current release is entirely different from that, so people would definitely be confused. This is one reason why they skipped version 6.






Access texbox values using POST method in PHP

Things you need to know first.

POST method:

There are two method to request or submit data from specific resources (GET and POST). Here I'm going to discuss about POST method with a pretty simple example.

In order to use POST method to submit data, the value of the method attribute must be POST in the FORM tag.

<FORM method = "POST">
</FORM>

POST method submits the data to the same php file or another php file. It depends on the action attribute of your form tag.


<FORM method = "POST" action= "file2.php">
</FORM>

Leaving the action attribute null will submit the data to the same php file. Like, 

<FORM method = "POST" action= "">
</FORM>

Input Box

Input box must have the proper attributes. The important attribute needed for post method is "name". Each input box should have an unique name.

For example:

<input type = "text" name = "input_value1">
<input type = "text" name = "input_value2">
<input type = "text" name = "input_value3">
<input type = "text" name = "input_value4">
<input type = "submit" name = "submit_data">

Access value from the form 

When the data are submitted from the form, data are ready to get accessed in the target file. To check whether the request method is POST. 

if($_SERVER['REQUEST_METHOD'] == "POST)

(or)

if($_POST)

Assign all the variables inside this if condition, to prevent the "undefined variable" error.

To store the values of the text box to PHP variables,

$variable_name = $_POST['name_of_inputbox'];

Eg) $name = $_POST['input_name'];
       $email = $_POST['input_email'];

You can display the data by using 'echo'
 echo $name;
You can directly display the values without storing in variables also, but it's time consuming and leads to lengthy codes.
echo $_POST['input_name'];

Note: $_POST is a pre-defined associative array variable. When we submit the form, the form data are stored in this array with respective index names.

Example Program:


<form method="POST" action="">
<input type="text" name="input_name" placeholder="Name"></br>
<input type="text" name="input_username" placeholder="Username"></br>
<input type="text" name="input_email" placeholder="Email"></br>
<input type="password" name="input_password" placeholder="Password"></br>
<input type="submit" name="submit_data" value="Submit">
</form>
<?php 
if($_SERVER['REQUEST_METHOD'] == "POST"){
//storing variables
$name = $_POST['input_name'];
$username = $_POST['input_username'];
$email = $_POST['input_email'];
$pass = $_POST['input_password'];

//Display the values
echo $name."<br>";
echo $username."<br>";
echo $email."<br>";
echo $pass."<br>";

}
?>
Values are stored in the variables now, now go ahead and store it in your database table. Click here to learn how to store data in MySQL database . Cheers!