Search posts, tags, users, and pages
var_dump((int) "test" == "test"); because 0 has to be equal to "test" ;D ... obviously :)
Are you shure? Can you explain why is wrote like this?
Ivijan (Stefan) Stipic well it's defined that (int) 'test' will be zero php.net/manual/en/language.types.string.php
hashbangcode.com/blog/string-equals-zero-php
and since it tries to convert the string to an integer as well and it is 0 as well it returns true
php > var_dump(0 == "asd");
php shell code:1:
bool(true)
so now to the fun part:
php > var_dump(1 == "asd");
php shell code:1:
bool(false)
but
var_dump((0 == "1asd"));
php shell code:1:
bool(false)
this can get tricky if you compare
php > var_dump((1 == "1asd"));
php shell code:1:
bool(true)
which is because the first part of the string is a number within the base 10 and it will take any amount of symbols (numbers) before it hits an invalid symbol
php > var_dump((int) "1asd");
php shell code:1:
int(1)
php > var_dump((int) "123asd");
php shell code:1:
int(123)
good enough ? :)
Yes, this is great explanaion but still is out of point. What my check doing in general? What I trying to get absolute from $something ?
Thi check have one great purpose what is not usual but is totaly logical and usefull.
Ivijan (Stefan) Stipic i already voted because I know what it does ;) i just found it funny the cast comparison without the is_numeric check looses semantic meaning and that is_int check is not working .... gg your question is not hard to me at all
but to be fair it is very useful, thank you :)
Here is one great example of this approach:
class Something
/*.....*/
public function set_orders_id($id){
if(is_numeric($id) && (int)$id == $id)
$this->orders_id = (int)$id;
else
$this->orders_id = 0;
return $this->orders_id;
}
/*.....*/
}
Ivijan (Stefan) Stipic the question to me is why check the contract inside of the method ? -> enforce it before ! -> function set_orders_id(int $id): int
that's how I code at least. you could ofc add a fair amount of if conditions but I like my ruleset strickt so I don't make to many mistakes and php5 is already deprecated.
but you're right it's a good example where it would make sense in <php7
j Well, I give that like example. Is better to use strict rules inside function arguments but imagine that we expect in that function integer, boolean, float and string. Integer is for ID, float is for price, boolean is for email send and string is for some message. We can use same funcion and simple if/else to separate data and use same function for various things. Is stupid example but I figureout it before some times where I was forced to use same string for various data like I decribe above. In that my problem I was not able to add additional argument or aditional string, just must expect something from database and do with that how I know and separate it properly.
I first time use this in my 14 years of practice.
Ivijan (Stefan) Stipic I think it's a good example, esp if we don't use "declare(strict_types=true)" in all files! :) assertion is always better than assumption in my opinion ! :)