Showing posts with label operator. Show all posts
Showing posts with label operator. Show all posts

Thursday, 8 April 2021

Php: ++, --: increment and decrement operators

++ Increment operator; increments a value by 1

-- Decrement operator; decrements a value by 1

 

Pre increment

Syntax:

++$variable_name

 

Example

++$a

     

Pre increment operator increments the variable value by 1 immediately.

 

pre_increment_demo.php

#!/usr/bin/php

<?php
$a = 10;

echo "Value of a is " . $a . "\n";
echo "Value of ++a is " . ++$a . "\n";
echo "Value of a is " . $a . "\n";
?>

 

Output

$./pre_increment_demo.php 

Value of a is 10
Value of ++a is 11
Value of a is 11

 

Post increment

In case of post increment operator variable is evaluated first, and increment will be done later.

 

Syntax:

$variable++

 

Syntax

$a++

 

post_increment_demo.php

#!/usr/bin/php

<?php
$a = 10;

echo "Value of a is " . $a . "\n";
echo "Incrementing a twice in the same line " . $a++ . "\t" . $a++ . "\n";
echo "Value of a is " . $a . "\n";
?>

 

Output

$./post_increment_demo.php 

Value of a is 10
Incrementing a twice in the same line 10    11
Value of a is 12

 

Pre decrement

Syntax:

--$variable_name

 

Example

--$a

     

Pre increment operator decrements the variable value by 1 immediately.

 

pre_decrement_demo.php

#!/usr/bin/php

<?php
$a = 10;

echo "Value of a is " . $a . "\n";
echo "Value of --a is " . --$a . "\n";
echo "Value of a is " . $a . "\n";
?>

 

Output

$./pre_decrement_demo.php 

Value of a is 10
Value of --a is 9
Value of a is 9

 

Post decrement

In case of post decrement operator variable is evaluated first, and decrement will be done later.

 

Syntax:

$variable_name--

 

Example

$a--

 

post_decrement_demo.php

#!/usr/bin/php

<?php
$a = 10;

echo "Value of a is " . $a . "\n";
echo "Incrementing a twice in the same line " . $a-- . "\t" . $a-- . "\n";
echo "Value of a is " . $a . "\n";
?>

 

Output

$./post_decrement_demo.php 

Value of a is 10
Incrementing a twice in the same line 10    9
Value of a is 8

 

 

 

 

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Tuesday, 6 April 2021

Php: <=>: Spaceship operator

Spaceship operator return

a.   0 if both the operands are same

b.   -1 if the left operand is less than right one

c.    1 if the left operand is greater than right one

 

spaceship_operator_demo.php

#!/usr/bin/php

<?php
$a = 11 <=> 11; // 0
$b = 11 <=> 22; // -1
$c = 22 <=> 11; // 1

$d = 'a' <=> 'a'; // 0
$e = 'a' <=> 'b'; // -1
$f = 'b' <=> 'a'; // 1

echo "11 <=> 11 : $a \n";
echo "11 <=> 22 : $b \n";
echo "22 <=> 11 : $c \n\n";

echo "'a' <=> 'a' : $d \n";
echo "'a' <=> 'b' : $e \n";
echo "'b' <=> 'a' : $f \n";

?>

 

Output

$./spaceship_operator_demo.php 

11 <=> 11 : 0 
11 <=> 22 : -1 
22 <=> 11 : 1 

'a' <=> 'a' : 0 
'a' <=> 'b' : -1 
'b' <=> 'a' : 1

 

 

 

 

Previous                                                    Next                                                    Home