All Posts Tagged ‘php


Void return types coming in PHP 7.1!

Leave a reply

As we all know the fact that we will have the ability to declare the return types from PHP 7 onwards, there might also be situations where you may not want to return anything, and therefore, an RFC regarding void return types was initiated, which requested the implementation of the void return type for methods or functions which will indicate that there is nothing to be returned by the function or method.

You could declare the return type as void for a function or method this way:

function returnNothing(): void {
    //perform operations

After a vote between some PHP internal members, this idea received 37 positive votes against 7 negative, and therefore, was decided as approved for PHP 7.1 as you can see in this message here.


PHP7, are you ready for it?

Leave a reply

You guys may very well be aware of the fact that PHP7 is coming by the end of this year which is something very significant to every person out there who uses PHP. May it be a freelancer, a student or a company, everyone right now is on about PHP7’s release.

1. Scalar type declarations. You guys may be aware that Scalar type declaration is a feature of PHP7, but what really is it? Well, with this feature, you can now type hint data types where-as in previous versions of PHP, it was only possible to type hint classes and arrays.

2. Return type declarations. In PHP7, not only can you type hint data types, you can also define the function or method return data type. Which is the type of data your function or method is going to return after it is done executing. For example, if your function or method returns a sum of two integers, then the return type is obviously integer.

3. Null coalesce operator. With this brand new null coalesce operator, you can make your code more shorter when using ternary operators. You may do something like $age = isset($_GET['age']) ? $_GET['age'] : null); before, but you no longer need to write that much any more! All you have to do to achieve that is to write $age = $_GET['age'] ?? null; and that is it! That does the trick for you right away.

4. Spaceship operator. This is another cool operator coming in PHP7. The symbol or the notation for this operator is <=> and it is used to compare two integers, floats or strings. If both of the values are equal, then it returns a 0. If the left value is smaller than the right value, then it returns a -1 and when the left value is greater than the right value, then it returns a positive 1. You might be wondering how the strings are determined to be greater or smaller in value, well, it is determined by the ASCII value they hold.

5. Constant arrays. With this, you can define an array which is a constant using the define() function. Until PHP 5.6, it was only possible to achieve this using the “const” keyword. The syntax is as follows:

define('PEOPLE', [

Sounds cool, yeah?

6. Anonymous classes. You might have heard of anonymous classes. If you haven’t, then, anonymous classes are classes which are basically not defined as normal classes. They are basically just use and dispose classes. An example would be:

    new class {
        public function makeSound()
            echo "Woof!";

So, as you see in the above code, a class is just created as well as instantiated at the same time and passed onto a method, and as soon as it serves it purpose, it disappears from the context.

Although these are not the only features going to be implemented in PHP7, I am going to stop here. If you want to know the complete list, then you can go ahead and take a look here at the PHP’s official site.

Credits: PHP Manual for information regarding PHP7.