Jek Jek - 7 months ago 20
PHP Question

PHP meaning of '//'

I've been converting some code from other languages (that are using algorithms) and adding them into PHP to some class code.

I came across some code that did the following line: (Python)

decNumber = decNumber // 2


I thought that it meant divide by two, but the outcome of adding this to my code was not as expected.

If this doesn't mean
/2
in PHP form then is there a PHP equivalent for this?

Answer

In PHP // means single line comment. The characters after // won't be executed.

You can use type casting in PHP for achieving the same. Below code will give you integer quotient.

<?php
$decNumber = 3;
$decNumber = (int)($decNumber / 2) ;
echo $decNumber;
?>

Output

1