This question already has answers here:
php filter_var fails with zero on FILTER_VALIDATE_INT
(2 answers)
Closed 5 years ago.
I had issue about FILTER_VALIDATE_INT function when i put 0 as entry value it show 'error' instead of 'ok':
$delivery = 0;
if (filter_var($delivery,FILTER_VALIDATE_INT)) {
echo 'ok';
}else{
echo 'error';
}
Compare with false, because PHP consider zero returning as false
if (false !== filter_var($delivery,FILTER_VALIDATE_INT)) {
Check for strict type check it will work,
$delivery = 0;
if (filter_var($delivery,FILTER_VALIDATE_INT) !== false) {
echo 'ok';
}else{
echo 'error';
}
Demo
Related
This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 1 year ago.
I have 2 strings $verify and $test. I want to check if $test contains elements from $verify at specific points. $test[4] = e and $test[9] = ]. How can I check if e and ] in $verify?
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if ($test[4] == $verify AND $test[9] == $verify) {
echo "Match";
} else {
echo "No Match";
}
Use strpos() https://www.php.net/manual/en/function.strpos.php
https://paiza.io/projects/n10TMV4Ate8-vtzioDrsMg
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (strpos($verify, $test[4]) !== false && strpos($verify, $test[9]) !== false) {
echo "Match";
} else {
echo "No Match";
}
?>
As of PHP8 you can use str_contains()
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (str_contains($verify, $test[4]) && str_contains($verify, $test[9])) {
echo "Match";
} else {
echo "No Match";
}
?>
Thanks to #symlink for the code snippet I butchered
This question already has answers here:
whats the difference in parentheses in IF statements?
(9 answers)
Closed 2 years ago.
I have this simple if statement but I do not get the results I expect. If all three vars match then I get not supported as expected. However I expect that as soon as I change one of the vars to a value that is not in the if statement, e.g. $Main = "SomethingElse", for the if statement to not match and therefor echo supported. However, supported is only returned if all three vars do not match the if statement.
Why does this happen?
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && $Backup == "Back_Other" || $Backup == "Back_None") {
echo "not support";
} else {
echo "supported";
}
In your example the if statement will always return true if the value of $backup is set to Back_None.
Try using below code. Here it will check $backup value first using || operator and then it will check the result with && operator
$Main = "Main_Other";
$Backup = "Back_None";
$online = "no";
if ($online == "no" && $Main == "Main_Other" && ($Backup == "Back_Other" || $Backup == "Back_None")) {
echo "not support";
} else {
echo "supported";
}
This question already has answers here:
Is there a way to check if a function exists within a class?
(3 answers)
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I want to use some code only if the method getProductgroup exists.
My first approach:
if(isset($item->getProductgroup())){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
I got the error message:
Compile Error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead)
if(($item->getProductgroup())!==NULL){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
But like this I also get an error message:
Attempted to call an undefined method named "getProductgroup" of class
"App\Entity\Documents".
You can use the function method_exists to check if the method is existing in a class or not. for example
if(method_exists('CLASS_NAME', 'METHOD_NAME') )
echo "it does exist!";
else
echo "nope, it is not there...";
In your code try
if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup)
{
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
}
}
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a few lines of code
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case = 1)
{
$message['a']="co";
}
if ($case = 0)
{
$message['a']="to";
}
echo $message['a'];
It will echo "co". Why is this? The file_put contents puts "0". However the if statement thinks it is 1 for some reason...
You are doing wrong in if condition. You doing assign instead of comparison. So here is the solution.
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case == 1)
{
$message['a']="co";
}
if ($case == 0)
{
$message['a']="to";
}
echo $message['a'];
you have to use the comparison operator "==" when comparing values: otherwise you are assigning values (in this case you were assigning $case to be 1 and then the message was "co".
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case == 1)
{
$message['a']="co";
}
if ($case == 0)
{
$message['a']="to";
}
echo $message['a'];
This question already has answers here:
How can I check if an array contains a specific value in php? [duplicate]
(8 answers)
Closed 9 years ago.
How to check php variable with array?
When $user_check = "ccc";
<?php
$user_group = array('aaa' , 'bbb' , 'ccc' , 'ddd');
$name_group = join("','",$user_group);
if ($name_group != $user_check) {
echo "not found."
} else {
echo "found."
}
?>
Try in_array()
if (in_array($user_check,$user_group))
{ echo "found."; }
else
{ echo "Not found."; }
Check the documentation on this link.
Use in_array Function
if ( in_array($user_check, $user_group) ) {
echo "found."
} else {
echo "not found."
}