php if statement null and 0? - php

i'm having trouble executing my if statement
if($parent == $page->parent)
my dollar $parent = null and my $page->parent = 0 how do i fix this that they are equal?
i think it's a problem with the fact it doesn't know that null is equal to 0
$page = Page::find($id);
$parent = Input::get('parent'); // Null
i hope you guys can help me out i have to figure this out
here's my controller just in case you wan't to take a look at it
public function updateMenu($id)
{
$page = Page::find($id);
$parent = Input::get('parent');
$new_order = Input::get('index');
if($parent == $page->parent)
{
if($page->order_id > $new_order)
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '<', $page->order_id)
->increment('order_id');
}
else
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '>=', $page->order_id)
->decrement('order_id');
}
}
else
{
DB::table('pages')
->where('parent',$parent)
->where('order_id', '>=', $new_order)
->increment('order_id');
}
$page->order_id = Input::get('index');
$page->parent = Input::get('parent');
$page->save();
return $id;
}

I dont think 0 can be equals with NULL. maybe if($parent == $page->parent || ($parent == null && $page->parent == 0)) more helpful

Use the strict equality operator: === instead of ==
localhost> = 0 === null
false
localhost> = 0 == null
true

You can use:
if(is_null($parent) && ($page->parent==0))
{
}

Related

Writing a function in laravel

I have the following function which fetch some data related to jobs from database. The user can search for jobs with job title / keyword, city and/or category. The user can either choose one option, e.g. searching jobs only by title, or by category. or he can use all options for deep search. Below is my function:
public function jobsearch(Request $request)
{
$keyword = htmlspecialchars($request->input('keyword'));
$city_id = $request->input('city_id');
$category_id = $request->input('category_id');
if($keyword !== '' && $city_id != 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('city_id', $city_id)->get();
}
elseif($keyword !== '' && $city_id == 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->get();
}
elseif($keyword == '' && $city_id != 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('category_id', $category_id)->where('city_id', $city_id)->get();
}
elseif($keyword !== '' && $city_id == 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->get();
}
elseif($keyword == '' && $city_id == 0 && $category_id != 0)
{
$data = DB::table('job_details')->where('category_id', $category_id)->get();
}
elseif($keyword == '' && $city_id != 0 && $category_id == 0)
{
$data = DB::table('job_details')->where('city_id', $city_id)->get();
}
else
{
$data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->where('city_id', $city_id)->get();
}
foreach($data as $data)
{
echo $data->job_title.'<br>';
}
}
As you can see the function is too much messy with many if and elseif statements. My question is if there is any way to write the given function in clean way? How would you write the given function in your style? Please Help.
You're really missing out on the best parts of Laravel's query builder.
public function jobsearch(Request $request) {
// htmlspecialchars makes no sense here
$keyword = $request->input('keyword');
$city_id = $request->input('city_id');
$category_id = $request->input('category_id');
$query = DB::table('job_details');
if($keyword) {
$query->where('job_title', 'like', '%'.$keyword.'%');
}
if($city_id) {
$query->where('city_id', $city_id);
}
if($category_id) {
$query->where('category_id', $category_id);
}
$results = $query->get();
foreach($data as $data) { ... }
}

PHP Zero integer is being evaluated as false

As the title says, PHP seems to be evaluating the integer value 0 as false.
Take for example this snippet:
http://sandbox.onlinephpfunctions.com/code/13d885fb68359a3154999c2ef85db7c913c49bc5
<?php
if($exists = checkDup()){
echo $exits;
}
else{
echo "error!";
}
function checkDup ($foo = 'blah', $bar = 'blah'){
if ($foo == $bar) return (int) 0;
return false;
}
As you can see, despite casting the reply as an int PHP parsing the return as false which in incorrect.
PHP is evaluating a lot to false ;)
For example null, '', 0
You have to include a type check as well, you can do so by using === or !==
$exists = checkDup();
if($exists !== false){
echo $exits;
}
else{
echo "error!";
}
function checkDup ($foo = 'blah', $bar = 'blah'){
if ($foo == $bar) return 0;
return false;
}
You should use if($exists = checkDup() !== false)
0 == false; //true
0 === false; //false
When you don't specify the a boolean expression in the if, it will execute the == operator

Different condition between 4 variables

Discount array
$Discount=arrray(
0=>array('FromArea'=>0,'ToArea'=>0,'Master'=>0,'Slave'=>0),
1=>array('FromArea'=>0,'ToArea'=>10,'Master'=>5,'Slave'=>0),
2=>array('FromArea'=>5,'ToArea'=>0,'Master'=>0,'Slave'=>8),
3=>array('FromArea'=>0,'ToArea'=>0,'Master'=>1,'Slave'=>2),
4=>array('FromArea'=>0,'ToArea'=>1,'Master'=>7,'Slave'=>5),
...
)
I want get discount amount base on input parameter of function.
If !empty(parameter) then check it in array
Like this
function DiscountAmount($FromArea, $ToArea, $Master, $Slave){
foreach ($Discounts as $R) {
if (!empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && !empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master && $R["Slave"] == $Slave)
return true;
} else if (!empty($FromArea) && !empty($ToArea) && !empty($Master) && !empty($Slave)) {
if ($R["FromArea"] == $FromArea && $R["ToArea"] == $ToArea && $R["Master"] == $Master && $R["Slave"] == $Slave)
return true;
}
...
}
}
$amout=DiscountAmount(0, 0, 0, 0);
$amout=DiscountAmount(1, 0, 0, 0);
$amout=DiscountAmount(0, 1, 0, 0);
$amout=DiscountAmount(0, 0, 1, 0);
$amout=DiscountAmount(0, 0, 0, 1);
$amout=DiscountAmount(1, 1, 0, 0);
$amout=DiscountAmount(1, 0, 1, 0);
$amout=DiscountAmount(1, 0, 0, 1;
$amout=DiscountAmount(1, 1, 1, 0);
$amout=DiscountAmount(1, 1, 0, 1);
$amout=DiscountAmount(1, 1, 1, 0);
$amout=DiscountAmount(1, 1, 1, 1);
....
Online Demo
But with this way you must check very case. Is there a simpler solution to do it?
function DiscountAmount($FromArea, $ToArea, $Master, $Slave)
{
//This line's simply to save time: if all fields are empty, no need to check further
if(empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave))
return true;
foreach ($Discounts as $R)
if (equiv($FromArea, $R["FromArea"]) &&
equiv($ToArea, $R["ToArea" ]) &&
equiv($Master, $R["Master" ]) &&
equiv($Slave, $R["Slave" ]))
return true;
return false;
}
function equiv($Field,$OtherField)
{
if (empty($Field)) return true;
else return $Field == $OtherField;
}
This ought to be significantly less clunky.
function DiscountAmount($FromArea = null, $ToArea = null, $Master = null, $Slave = null) {
global $Discounts; //pull discount array into the function
$argsCount = count(func_get_args()); //the number of arguments filled in
$checkArray = array(
'FromArea' => $FromArea, 'ToArea' => $ToArea, 'Master' => $Master, 'Slave' => $Slave
);
while (count($checkArray) < $argsCount) {
array_pop($checkArray);
}
foreach ($Discounts as $R) {
if ($checkArray == $R) { //checks if all key/value pairs are equal
return true;
}
}
return false;
}
Try with a regular expression:
function DiscountAmount($FromArea = null, $ToArea = null, $Master = null, $Slave = null){
if(empty($FromArea) && empty($ToArea) && empty($Master) && empty($Slave)) return true;
global $Discounts;
$json = json_encode($Discounts);
$reg = "/\{";
if(!empty($FromArea)) $reg .= "(?:[^\}]*\"FromArea\":{$FromArea})";
if(!empty($ToArea)) $reg .= "(?:[^\}]*\"ToArea\":{$ToArea})";
if(!empty($Master)) $reg .= "(?:[^\}]*\"Master\":{$Master})";
if(!empty($Slave)) $reg .= "(?:[^\}]*\"Slave\":{$Slave})";
$reg .= "[^\}]*\}/";
return preg_match($reg, $json);
}
I think this will eliminate the redundancy:
function DiscountAmount($Parameters){
foreach ($Discounts as $R) {
foreach ($Parameters as $PK => $P) {
$CheckFlag = true;
if (!empty($P)) {
if ($R[$PK] != $P) {
$CheckFlag = false;
break;
}
}
}
if (!empty($CheckFlag)) { return true; }
}
return false;
}
Assumptions:
Based on the question's code and the accepted answer:
If any of the arrays has non-empty values that match the input values, the function must return true
If all input arrays are empty, function must return true
Test
http://ideone.com/otiUJw

Function returning boolean only returns false

I've a simple function that apprently puts me up with a lot of trouble. The function is:
function valid_mail($email) {
$atpointers = strstr($email, "#");
$spacepointers = count(explode(" ", $email));
$dotpointers = strstr($email, ".");
$ltpointers = strstr($email, "<");
$gtpointers = strstr($email, ">");
$illegalpts = $ltpointers + $gtpointers;
if($atpointers >= 2 || $dotpointers == 0 || strlen($email) <= 6 || $illegalpts >= 1) { return false; } else { return true; }
}
And calling it in the context:
if(valid_mail($email) === false) { // Code } else { // Code }
The problem is apparently it only returns false. Any ideas for why this happens ?
strstr returns a substr of the string being checked, not a length. Use strpos instead.

php | Simple question on conditional statement

I'm new to php and I would like to have a conditional statement like this:
if ($foo != 'Any') { $condition .= '$this_foo == $foo &&';}
if ($bar != 'Any') { $condition .= '$this_bar == $bar &&';}
if ($txt != 'Any') { $condition .= '$this_txt == $txt &&';}
$condition .= '$num1 > 0 && $num2 < 1000';
if (...[php would parse the $condition variable])
{
someoperations
}
What is the proper syntax for the if statement to parse the variable $condition? So the conditional statement depends on the other variables and to prevent a long nested conditional statement.
Thanks in advance!
Well it's not exactly parsing, but you could evaluate your condition as the code is executed.
$condition = true;
if ($foo != 'Any') { $condition = $condition && ($this_foo == $foo);}
if ($bar != 'Any') { $condition = $condition && ($this_bar == $bar);}
if ($txt != 'Any') { $condition = $condition && ($this_txt == $txt);}
$condition = $condition && ($num1 > 0 && $num2 < 1000);
if ($condition)
{
someoperations
}
So let's say that $foo != 'Any' is true, that would result in
$condition = true && ($this_foo == $foo) && ($num1 > 0 && $num2 < 1000);
Let's pretend $this_foo == $foo, $num1 == 45 and $num2 == 2300
$condition = true && true && (true && false);
$condition = false;
and your if won't execute.
I believe what you want is
if (eval("return " . $condition)) {...}
Making sure to check for the FALSE case if the parsing fails.

Categories