Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 28, 2024 01:26 am GMT

30 tips of code PHP - Types

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 
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.

Original Link: https://dev.to/razielrodrigues/30-tips-of-code-php-types-3j20

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To