/ php

Quickie: a = b || c

This little post can be handy specially for full-stack devs, who use JS and PHP. PHP being a requirement here.

Do you know what happens when you assign OR-logical expression to a variable? Specifically in PHP, as it acts differently from other languages.

Python:

result = "hello" || "world"
print(result)

JavaScript:

result = 'hello' || 'world';
console.log(result);

In python and javascript the result is "hello".

PHP:

$result = 'hello' || 'world';
echo $result;

But in PHP you get true.
Don't forget about this when you switch contexts or rewrite code to/from PHP.

Tags:
#php #python #js #quickie

TvK

IT and languages. Feel free to be a grammar-nazi and correct my English (or any other language).

Read More