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."
}
Related
This question already has answers here:
Checking if ANY of an array's elements are in another array
(8 answers)
Closed 9 months ago.
I have 2 php array variables. Variables like below.
$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];`
I want to check whether values of $arrIntRequestPropertyIds are in $arrIntPropertyIds without loop.
So I tried like below.
if( count(array_diff($arrIntPropertyIds,$arrIntRequestPropertyIds))==0 ) {
echo 'invalid property Id';
} else {
echo 'Valid property Id';
}
If all OR any of the value OR atleast 1 value is/are there in first array then it should show message 'invalid'.
All you have to do is change the order in which you present the arrays to the array_diff() function and then change the if accordingly
$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];
if( count(array_diff($arrIntRequestPropertyIds, $arrIntPropertyIds))==0 ) {
echo 'Valid property Id';
} else {
echo 'invalid property Id';
}
Use array_diff. Simplest way to check this would be the following code
$containsValues = !array_diff($arrIntRequestPropertyIds, $arrIntPropertyIds);
Would return true or false
<?php
$arrIntPropertyIds = [ 1,2,3,4,5,6,7,8,9,10];
$arrIntRequestPropertyIds = [ 3, 5, 7];
$intersect = count(array_intersect($arrIntPropertyIds, $arrIntRequestPropertyIds));
if ($intersect == count($arrIntRequestPropertyIds)) {
echo '$arrIntPropertyIds include all of $arrIntRequestPropertyIds';
} elseif ($intersect > 0) {
echo '$arrIntPropertyIds include part of $arrIntRequestPropertyIds';
} else {
echo '$arrIntPropertyIds not include any of $arrIntRequestPropertyIds';
}
run PHP online
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:
MySQL - count total number of rows in php
(11 answers)
Closed 4 years ago.
the code below is display the else statement, but when $getlogin == 0 it is not displaying
while ($getlogin = mysql_fetch_array($checklogin)) {
if ($getlogin == 0) {
echo "NOTHING TO DISPLAY";
}
else{
echo $getlogin['username'];
echo $getlogin['password'];
}
}
$checklogin = mysql_query($queryContents);
if(mysql_num_rows($checklogin)== 0){
echo "NOTHING TO DISPLAY";
}
else{
while($getlogin = mysql_fetch_array($checklogin)) {
var_dump($getlogin);
}
}
you can use php empty function also to check empty
like this
if (empty($checklogin)) {
echo 'NOTHING TO DISPLAY';
}else{
while($getlogin = mysql_fetch_array($checklogin)) {
var_dump($getlogin);
}
}
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
This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
<?php
if ($user->getProfile()->get('title')="Canon"); {
echo "Test1"; }
else {
echo "Test2"; }
?>
This is causing my site to break, is there an obvious mistake?
Thank you.
= is an assignment, you want == for a comparison.
You also shouldn't have a ; between ) and {.
This will work:
<?php
if ( ($user->getProfile()->get('title')) == "Canon" ){
echo "Test1";
}
else {
echo "Test2";
}
?>
First of all You need a comparison '==' and not an assigning '='
And then You have a syntax error with a ';' after the condition
You need to change the = sign to == as the first one assigns a values while the latter one compares it.
And also, you don't need to terminate the if statement with a semicolon
if ($user->getProfile()->get('title') == "Canon") /* note that there is no semicolon here */
{ echo "Test1"; }
else
{ echo "Test2"; }
<?php
if ($user->getProfile()->get('title')=="Canon") {
echo "Test1";
}
else {
echo "Test2";
}
?>
Try this.