This question already has an answer here:
I'm trying to convert this PHP function to Objective-C. I can just barely read PHP to figure out what the code does. Came across this line and stopped:
$nr = (strlen($nr)-3>0)?substr($nr, 0, strlen($nr)-3):"";
What do the ?
and :
do here? I read the documentation to see what strlen
and substr
do but haven't been able to figure out how this comes together and what the new value of $nr
is.
</div>
The ?:
is a ternary operator. The expression x ? y : z
means if x then y else z
. In this case, it would translate to something like
if strlen($nr) - 3 > 0:
$nr = substr($nr, 0, strlen($nr) - 3)
else:
$nr = ''
This is not Python, but if you were to break it into psuedo-Python syntax it would be
if strlen($nr) - 3 > 0:
$nr = substr($nr, 0, strlen($nr)-3)
else:
$nr = ""
In various languages, the ?
operator is a ternary operator. This syntax does not exist in Python. The above is how you would write such a statement, though you'd have to correct the syntax in each of the if blocks, as well as the if condition.
it will translate to
if (strlen($nr)-3>0) then value of $nr is substr($nr, 0, strlen($nr)-3)
else it is ""
And i dont think the code is in python it looks like php or perl.