This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 7 years ago.
I have the following code:
<?php
$starting_id = 0;
$params = array ('val' => $starting_id);
echo parse_params ($params);
function parse_params ($params)
{
$query = ' WHERE ';
if ($params['val'] === NULL) {
$query .= ' IS NULL';
return $query;
}
if ($params['val'] == 'NOT NULL') {
$query .= ' IS NOT NULL';
return $query;
}
return $query.' = '.$params['val'];
}
When I run it, I expect to see this:
WHERE
instead, I get the following:
WHERE IS NOT NULL
Any ideas why?
According to the PHP type comparison tables,
$var = 0 ; // (int)
if you compare $var == "string",
it will return true, you need to type check for this
$var === "string"
check php type comparison
Related
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:
PHP "If true" test on array always returns true?
(1 answer)
Is an array considered as boolean true in php?
(6 answers)
Closed 2 years ago.
I receive an array from a method, and I check the value of it. If the array equals to true, it should do something, but if not, it has to do something else. Here's my if statement :
$existence_status = $model->getReportExistenceStatus($validFormData['start_date'], $validFormData['end_date'], $validFormData['start_hour'] . ":00", $validFormData['end_hour'] . ":00");
var_dump($existence_status);
if ($existence_status) {
// Report was not found, continue processing a complete API request
// Get the report from the model
$report_data = $model->getReport($validFormData['start_date'], $validFormData['end_date'], $validFormData['start_hour'], $validFormData['end_hour'], $access_token);
// Set the data on the chart
if ($report_data !== false) {
$chart_status = true;
$start_date = $validFormData['start_date'];
$end_date = $validFormData['end_date'];
$start_hour = $validFormData['start_hour'];
$end_hour = $validFormData['end_hour'];
$success = $report_data['success'];
$failed = $report_data['failed'];
} else {
$chart_status = false;
}
} else {
// Report was found, get the data and print them
$chart_status = true;
$start_date = $validFormData['start_date'];
$end_date = $validFormData['end_date'];
$start_hour = $validFormData['start_hour'];
$end_hour = $validFormData['end_hour'];
$success = $existence_status['success'];
$failed = $existence_status['failed'];
}
I just found out that if the returned value from the call of the getReportExistenceStatus is an array, it will go in the if instead of the else.
Here is the value of the $existence_status (got it from the var_dump) :
array (size=2)
'success' => int 100
'failed' => int 0
But it still goes in the if statement instead of the else.
I've found a way to get around that problem (I'm using php !is_array($existance_status) as condition), but that really perturbate me, because, afaik, an filled array is not considered as "true", no ?
Is that normal ?
This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I'm wondering if I could use the ternary operator for something like this:
var string = "";
if (something) {
string = "foo"
} else if (somethingElse) {
string = "bar";
} else if (bla) {
string = "pool";
} else if (xxxxx) {
string = "coffee";
} else {
string = "";
}
As far as I remember, I can do this in Java language:
String string = something?"foo":somethingElse?"bar":bla?"pool":xxxxx?"coffee":"";
But I'm not sure about PHP, I'm not even sure if it's OK to use ternary operator in this case or not.
E.g
if (something) {
string = "foo"
} else if (somethingElse) {
string = "bar";
} else if (bla) {
string = "pool";
} else if (xxxxx) {
string = "coffee";
} else {
string = "";
}
is equivalent to in PHP
(something) ? 'foo' : ((somethingElse) ? 'bar' : ((bla) ? 'pool' : ((xxxxx) ? 'coffe' : '')));
This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
I'm trying to compare two strings (one from my database and another supplied by the user) and see if they match! The problem I'm having is that they don't seem to match - even though the strings seems to be exactly identical?
My PHP code is below:
public function Verify($pdo, $id, $token) {
$prepsql = $pdo->prepare("SELECT * FROM Profiles WHERE id = '$id' LIMIT 1");
$prepsql->execute();
$currentrow = $prepsql->fetch();
$current = preg_replace("/[^a-zA-Z0-9]+/", "", $currentrow["token"]);
echo '<p>'.var_dump($current).'</p>';
echo '<p>'.var_dump($token).'</p>';
$token = preg_replace("/[^a-zA-Z0-9]+/", "", $token);
if ($current == null || $current = "") {
return false;
} else {
if (strcmp($token, $current) == 0) {
return true;
} else {
return false;
}
}
}
And here is the webpage output:
string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p>string(244) "CAAW4HRuZBuB4BACA7GffOAwLHgmLgMMLGQxDAw8IJDCwahZAh0S4wZAcP8Q9DmMwsDpBq7jFcH1EzUIsZBbhKov12utoYFQns0HhgB5xKLeDqtZBRqavaNjNSn7KAcObZAEcavQCRbGlVKZBArfDEHskBSR8qAoU543DVTZCOyHm5oYNDVafwHl0bAkc4jyIhh2YHEPaNpWGC0FhezsSidOgLjnfFq8CeLVxHH0nUZBMLgAZDZD"
<p></p><p>Not authenticated</p>
Not authenticated just means that this function is returning false...
What on earth am I doing wrong? As per advice given on other similar Stack Overflow answers, I've used the regex function to basically only keep alphanumeric characters but that has made no difference? It isn't a trimming issue either as that didn't work!
Any help would be much appreciated!
Thanks!
Here is your problem:
if ($current == null || $current = "") {
// ^ now `$current` is "", an empty string
You assign a new value to $current, an empty string.
You probably want something like:
if ($current == null || $current == "") {
// ^^ now you are comparing
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have Used recursive function to get the category full path like Access Control/CARDS/FOBS from above table structure but my recursive function return null value.
function xyz($id,$parent) {
if($parent == '0') {
//my code working fine
//return
}
else
{
$catid = $id; //here 25 coming
$cat_array = array();
$category_array = $this->Recursive($catid,$cat_array);
//echo $category_array;exit; getting Null result
return $category_array ;
}
}
function Recursive($catid,$cat_array)
{
$sql = mysql_query("Select bg_category_id,parent_id,title from categories_list
Where bg_category_id = '".$catid."'");
$result = mysql_fetch_array($sql);
array_push($cat_array,$result['title']);
if($result['parent_id'] != '0') {
$this->Recursive($result['parent_id'],$cat_array) ;
} else {
if(count($cat_array) > 0){
$k = implode("/",$cat_array);
}
//echo $k;exit; getting desired result FOBS/CARDS/Access Control
return $k;
}
}
You need to return the recursive function when you recurse, otherwise it will return nothing.
if($result['parent_id'] != '0') {
return $this->Recursive($result['parent_id'],$category_array) ;
}