Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What is the equivalent of PHP "=="

The question says it.

Here's a little explanation.

In PHP. "==" works like this

2=="2" (Notice different type)
// True

While in python:

2=="2"
// False
2==2
// True

The equivalent for python "==" in php is "==="

2===2
//True
2==="2"
//False

Million dollar question. What is php "==" equivalent to in python?

like image 287
Realdeo Avatar asked Apr 27 '14 12:04

Realdeo


People also ask

Can python be used instead of PHP?

In a word, PHP is meant for web and web only. Python, on the other hand, is infinitely more versatile. It can be used for web development, but also many, many other things.

Can I connect PHP to python?

You can run a python script via php, and outputs on browser. Basically you have to call the python script this way: $command = "python /path/to/python_script.py 2>&1"; $pid = popen( $command,"r"); while( ! feof( $pid ) ) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); } pclose($pid);

Which is better PHP or python Why?

Python is better than PHP in long term project. PHP has low learning curve, it is easy to get started with PHP. Compare to PHP Python has lower number of Frameworks. Popular ones are DJango, Flask.

Is PHP faster than python?

Each time a file is created or modified; Python converts the code into bytecode. This code compilation method makes Python quicker than PHP. PHP programmers can simply improve the speed of PHP applications by installing a variety of caching systems. By default, Python is a faster language.


2 Answers

Python doesn't coerce between types the way PHP does, mostly.

You'll have to do it explicitly:

2 == int('2')

or

str(2) == '2'

Python coerces numeric types (you can compare a float with an integer), and Python 2 also auto-converts between Unicode and byte string types (to the chagrin of many).

like image 193
Martijn Pieters Avatar answered Nov 18 '22 14:11

Martijn Pieters


There isn't one. You need to convert types before checking for equality. In your example, you could do

2==int("2")
like image 5
jrennie Avatar answered Nov 18 '22 14:11

jrennie