30 Tips Of Code PHP – Types

Rmag Breaking News

PHP is a less typed language it means PHP can convert string into integers or act in other types of conversion automatically. However, this is one of the singularities of PHP types, let’s dig in!

All types in PHP

null
bool (0,1,true,false)
int
float (floating-point number)
string
array
object
callable (functions)
resource (External libraries)

Digging into

Heredoc printing

Did you know that PHP has heredoc as a way to print things? that’s how it works!

echo <<<END a b c n END; # Then the indentation will persist when printing # You can also pass variables, functions and other interactions inside the string or even save the result in a variable “`
{% endraw %}
Powers of array Arrays are one of the most powerful types in PHP let‘s learn some cool stuff to do with arrays
{% raw %}
“`php # you can create array in that also $array=new ArrayObject(); # take a look how to destruct $destruct=[‘
test‘, ‘key‘ ]; [$test, $key]=$destruct; $destructAssociative=[ ‘name‘=> ‘Raziel‘];
[‘
name‘ => $myName] = $destructAssociative;

var_dump($test, $key, $destructAssociative);

# we also have spread operator mr. Javascript

$arrayUnpacking = […$destructAssociative, …$destruct];
var_dump($arrayUnpacking);

# pointers are cool
$point = [1,2,3];
$appointed = &$point;
$point[] = ‘hello‘;

# will add the same data here because the pointer
var_dump($appointed);

“`

– Enum

Don’t need to create constants inside classes anymore
{% raw %}

“`php

enum Suit {
case Suit;
}

“`
{% endraw %}

Declare types

An easter egg about declare(strict_types=1)

“Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller’s preference (coercive typing) will be respected, and the value will be coerced.”

Declare types

An easter egg about declare(strict_types=1)

“Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller’s preference (coercive typing) will be respected, and the value will be coerced.”
{% raw %}

“`php

#file with declare

function sum(int $a, int $b)
{
return $a + $b;
}

“`
{% endraw %}

The never return type

This one is such a gem, I don‘t know if I will see this return type being used, however I think is a great idea to know what it does, the never basically ignore the throw, exit or die function, when never is defined as a return, even if you call exit inside the function the system will continue instead of stopping.

“`php

function neverSay():never
{
echo ‘Nobody can stop me!‘;
exit;
}

function willContinue(): never
{
throw new Exception(‘Now way!‘);
}

try {
willContinue();
} catch (Exception $th) {
echo $th->getMessage();
}

“`

That’s all! I hope those tips can help you in your carrer and in your daily tasks, see you on my next articles.

Leave a Reply

Your email address will not be published. Required fields are marked *