LEARN PHP BASICS: Beginner's Guide For Coding by J TAM

LEARN PHP BASICS: Beginner's Guide For Coding by J TAM

Author:J TAM [TAM, J]
Language: eng
Format: azw3
Published: 2020-05-09T16:00:00+00:00


Let's take an example illustrating the spaceship operator in PHP.

<html>

<head>

​ <title>PHP <=> Operator or Spaceship Operator</title>

</head>

<body>

<?php

​ print (5 <=> 5);

​ echo "<br/>";

​ print (3 <=> 5);

​ echo "<br/>";

​ print (5 <=>3);

​ echo "<hr/>";

​ print ("C" <=> "C");

​ echo "<br/>";

​ print ("A" <=> "C");

​ echo "<br/>";

​ print ("C" <=> "A");

​ echo "<hr/>";

?>

</body>

</html>

PHP ?? or Coalescing Operator

The ?? operator in PHP is introduced in its 7th version, that is, PHP 7.

In PHP ?? or Coalescing operator, if the first operand of this operator is set then it returns its first operand, otherwise second operand will be returned by this operator.

Here is an example that demonstrates PHP Coalescing or ?? operator.

<html>

<head>

​ <title>PHP ?? Operator or Coalescing Operator</title>

</head>

<body>

<?php

​ $firstname = "Codes";

​ $lastname = "Cracker";

​ $fullname = $_POST['name'] ?? 'name is empty';

​ echo $fullname;

​ echo "<hr/>";

​ $name = $firstname ?? "firstname is not initialized";

​ echo $name;

​ echo "<hr/>";

​ $name = $surname ?? "surname is undefined";

​ echo $name;

​ echo "<hr/>";

​ $name = $fullname ?? "fullname is set previously";

​ echo $fullname;

?>

</body>

</html>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.