I am trying to print "Backed" when user id is in array.
Here is my code:
$backId = app()->user->model()->id;
$backers = MProjectBacker::model()->findAll('projectId=:pid', array(':pid' => $data->id));
foreach ($backers as $b) {
if (in_array($backId, $b, true)) {
echo "Backed!";
} else {
echo "Not Backed!";
}
}
But no result and no error.
"If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack."
But why use in_array function to this?
Why dont you just check the field where you store the $backId?
foreach ($backers as $b) {
if ( (int) $b['fieldNameWhereYouStoreTheBackId'] == (int) $backId ) {
echo "Backed!";
}
else {
echo "Not Backed!";
}
}
Related
I am writing a function, with the name max_number, which returns the maximum of a number
numbers variable. If one of the arguments is not a number, then it returns false.
Using the is_numeric () function. But the function always returns false. Will I do something wrong?
The code and this:
<?php
function max_number()
{
$args = func_get_args();
if( $args !== null && !is_numeric($args)){
echo "false";
}
else{
echo "Maximo number is: " . max($args);
}
}
max_number(1,2,3,4,5);
?>
In your code $args is an array and is_numeric($args) will always return false.
You need to check every element of $args individually.
<?php
function maxNum() {
$args = func_get_args();
return ($args === array_filter($args,'is_numeric'))?max($args):false;
}
echo maxNum(1,2,3,4,5); // 5
echo maxNum(1,"No!",3,4,5); // false
<?php
function max_number()
{
$args = func_get_args();
if( $args == null && !is_numeric($args)){
echo "false";
}
else{
echo "Maximo number is: " . max($args);
}
}
max_number(1,2,3,4,5);
?>
Inside a JSON file I have multiple keys with the name type and their values are numeric. What I'm trying to do is to check if the exact number exists. The problem is that if have two values with the same digit it shows me both TRUE. Eg 41 & 1.
What I tried so far
$regex = '/^1$/';
foreach ($value['events'] as $event) {
if ($event['type'] == $regex) {
echo 'Exist';
}
}
Thank you
You can use array_column() and in_array()
if (in_array(1, array_column($value['events'], 'type'))) {
echo "Exist";
}
I was typing the same as Barmar's answer but this is another alternative. Just filter out the ones that don't equal your number:
if(array_filter($value['events'], function($v) { return $v['type'] == 1; })) {
echo "Exist";
}
This would be a better approach if you needed to test more than one condition such as:
return ($v['type'] == 1 || $v['type'] == 2);
//or
return ($v['type'] == 1 && $v['other'] == 'X');
When I am displaying my array by using var_dump I get the following result:
array(1) { [0]=> NULL }
I want to apply a condition that when my array has a null value it should do something. I have tried using array[0]== NULL and array[0]= NULL inside my condition but it does not work. Can anyone tell me what could be the correct condition for it?
PHPs empty() checks if a variable doesn't exist or has a falsey value (like array(), 0, null, false, etc).
<?php
if (!empty($array[0])) {
echo "Not empty";
} else {
echo "empty";
}
?>
or by using is_null
<?php
if(is_null($array[0])) {
echo "empty";
} else {
echo "not empty";
}
?>
or
<?php
if($array[0] === NULL) {
echo "empty";
} else {
echo "not empty";
}
?>
You can do it by several ways:
if(is_null($array[0])) {}
or
if(!isset($array[0])) {}
or
if($array[0] === null) {}
By the way, == makes a comparison, = is an assignment (even in an if statement) and === compares values and type.
$arr = array();
if (!empty($arr)){
//do your code
}
else {
echo "Hey I'm empty";
}
Code ONE (WORK ARIGHT):
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
$pr = HELLO(3);
echo $pr;
It code work aright.
Then I wanted to do one function to process the data and output the result.
Code:
function out( $rel, $result ) {
if ( $rel == 1 ) {
print $result;
} elseif ( $rel == 2 ) {
echo $result;
} elseif ( $rel == 3 ) {
return $result;
} else {
return $result;
}
}
function Hello( $rel ) {
$res = mysqli("SELECT * FROM TABLE");
$result = $res->num_rows;
out( $rel, $result )
}
$pr = HELLO(3);
echo $pr;
But now code not work(not show results on line echo $pr;)...
Tell me please why i have error and how write aright?
P.S.: i not know that need use return before function.
Thanks all for my new knowledge.
You simply forgot to add return to out($rel,$result)
as it is right now, your Hello() function doesn't have return value.
You have not return the value in the second code.
you need to use like this:
return out($rel,$result).
The return is in the second function, second function returns the value to function first, now function first also needs to return, so u need to add return there too.
I wrote this:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true and pass1($b1) == true)
{
login2($a1, $b1);
}
else
{
echo "error!!!!";
}
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
{
echo "you are logged in";
}
else
{
echo "erorr";
}
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
{
echo "gooooooood?";
}
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
{
echo "goooooooood!!!!!!!!";
}
else
{
echo "bad";
}
}
login($a, $b);
and I know that pass() and user() are true because I changed their positions on the function login() and every time I did this the first argument was returned as true and it didn't check the second one. Does anyone know why this happens?
user and pass1 functions should return true or false, not echo out.
Your user and pass1 functions are not returning an explicit value, so they are implicitly returning the NULL value. As described on this page in the PHP manual the NULL type is converted to false when a boolean is expected. So both your user and pass1 functions return false every time.
The && and and logical operators in PHP use short-circuiting for efficiency (see the first code example on this page of the PHP manual) so in any and statement whose first operand evaluates to false it can never be possible for the whole and statement to evaluate to true, so the second operand (in the case of your code above, the second operand is the call to pass1($b1)) is never evaluated because it would be a waste of time to do so.
Which means you're seeing the user function being called, but never the pass1 function.
Try using this instead:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true && pass1($b1) == true)
login2($a1, $b1);
else
echo "error!!!!";
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
echo "you are logged in";
else
echo "erorr";
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
echo "gooooooood?";
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
echo"goooooooood!!!!!!!!";
else
echo "bad";
}
login($a, $b);