I'm working on something like this:
if ($varA != null & $varB = null){
$newthing = "Something else";
}
It doesn't seem to want to work. I've tried && for & but I can find a good reference for these and what they do.
EDIT: Going to try and clear up what my objective is.
I have three if statement that I want to execute whether or not some variables have value in an encoded URL. Those three statement are:
if ($lprice != null && $hprice == null){
$clauses[] = "MSTLISTPRC >= " . $lprice;
}
if ($hprice != null && $lprice == null)(
$clauses[] = "MSTLISTPRC <= " . $hprice;
}
if ($lprice != null && $hprice != null){
$clauses[] = "MSTLISTPRC BETWEEN " . $lprice . " AND " . $hprice;
}
The final if statement works, the first two don't. It's something with the PHP code itself because I'm testing it an echo to just output the query string and when I try to execute this, it fails. I don't know how to apply error handling so I haven't been using that.
You need to use && instead of & and == instead of =.
So:
if ($varA != null && $varB == null){
$newthing = "Something else";
}
if ($varA != null && $varB = null){
$newthing = "Something else";
}
this should be fine..
Related
I'm creating a function that has to check for several inputs and create a variable that I use to call a SQL query, the problem I'm facing is that I want to change a variable so that I don't use the SELECT call every time, but instead the AND call. How does one construct such a function? This is what I currently have.
if ($_GET['filtering2'] != "" || $_GET['filtering3'] != "" || $_GET['filtering4'] != "" || $_GET['filtering5'] != "" || $_GET['filtering6'] != "" || $_GET['filtering7'] != "" || $_GET['filtering7'] != "") {
function searchByColumn($values, $columnName)
{
$string = implode(" OR season LIKE ", $values);
if ($firstTime = 2) {
return "SELECT * FROM shrubs2 WHERE season LIKE $string";
} else {
return " AND WHERE season LIKE $string";
}
}
$colNames = array("season"); // can add here more column names
foreach ($colNames as $colName) {
$str .= searchByColumn($array_name, $colName);
$firstTime = 3;
}
}
if ($_GET['filtering8'] != "" || $_GET['filtering9'] != "" || $_GET['filtering10'] != "" || $_GET['filtering11'] != "") {
function searchByColumn2($values, $columnName2)
{
$string2 = implode(" OR 日照 LIKE ", $values);
if ($firstTime = 2) {
return "SELECT * FROM shrubs2 WHERE 日照 LIKE $string2";
} else {
return " AND WHERE 日照 LIKE $string2";
}
}
$colNames2 = array("日照"); // can add here more column names
foreach ($colNames2 as $colName2) {
$str .= searchByColumn2($array_name2, $colName2);
$firstTime = 3;
}
}
I was trying to change the variable $firstTime, but it never resulted as I wished for it to do.
first edit:
if($_GET['filtering8'] != "" || $_GET['filtering9'] != "" || $_GET['filtering10'] != "" || $_GET['filtering11'] != "" ){
function searchByColumn2($values, $columnName2) {
$string2 = implode(" OR 日照 LIKE ", $values);
if($firstTime != 3){
return "SELECT * FROM shrubs2 WHERE 日照 LIKE $string2";
$firstTime = 3;
} else{
return " AND WHERE 日照 LIKE $string2";
}
}
$colNames2 = array("日照"); // can add here more column names
foreach($colNames2 as $colName2) {
$str .= searchByColumn2($array_name2, $colName2);
}
}
and of course I've added:
global $firstTime;
since the variable is global you need to define the global by adding
global $firstTime;
on first line of searchByColumn and searchByColumn2 function. it will refer to global variable $firstTime
I have been working on an old app mostly dealing with upgrading from mysql to mysqli and removing depreciated functions. While debugging the mess I would occasionally have an error with in line hyperlink to edit or delete products. Examples
php?act=del&cat_id=5&bc=654321&ds=&src=app
php?act=del&cat_id=5&bc=&ds=ds12345&src=app
php?act=del&cat_id=5&bc=654321&ds=ds12345&src=app
Some had one part some had two
But then discovered that items in a category could have 2 different identifiers ‘ds’ or ‘bc’ or both and the links to edit or delete these items could have one or the other or both neither. Or could have different errors! This Code is what they pointed to.
// Original code
if ($_GET['act'] == 'del') {
$cat_id = $_GET['cat_id'];
$ds = $_GET['ds'];
$bc = $_GET['bc'];
if($cat_id == '' && $ds == '' || $bc == '') {
echo 'Error Cannot identify item for action!';
}
else {
//$db = mysqli_connect($db_host, $db_login, $db_pwd, $db_name);
// Do some SQL Stuff in cat_id that match ds or bc
}
I tried dozens of variations of the original
if($cat_id == '' && $ds == '' || $bc == '')
using || && equal to or not equal to and one way or another could never get one to be correct in all cases. Including the one suggested by trincot
if ($cat_id == '' && ($ds == '' || $bc == '' ))
Which works for most cases but allows db connect with ds=’’ and bc =’’
The code below I wrote works as needed for all cases of bad query strings But what I was hoping is someone might have a better way to do it.
// From GET -- Just to fix if($cat_id == '' && $ds == '' || $bc == '') {
$cat_id = 3;
$ds = 'bbb';
$bc = '';
// define some vars
$c = 'NO'; $d = 0; $b = 0; $t = 0; $s = 0;
if (is_numeric($cat_id)) {
$c = 'OK'; }
if ($ds != '') {
$d = 1;
}
if ($bc != '') {
$b = 1;
}
$t = $d + $b;
if ( $s == $t || $c != 'OK') {
echo 'Error Cannot identify item for action! ';
}
else {
echo ' OK two out three aint bad as long as one is cat ';
//$db = mysqli_connect($db_host, $db_login, $db_pwd, $db_name);
// Do some SQL Stuff in cat_id that match ds or bc
}
This is a working sample of what I came up with Thanks for looking any help would be greatly appreciated.
http://sandbox.onlinephpfunctions.com/code/fd32ad87ff42836660a59f826b8f08fa0f8d16f0
Here are the different examples I want to test against. And desired result I know I need to update other parts of the original code but for now I am just looking for an easier way to trigger My Error! before db connect!
example 1:
$cat_id = 3;
$ds = '';
$bc = '';
Result: Error Cannot identify item for action! \\ No Part number either ds or bc
example 2:
$cat_id = 'a';
$ds = 'ds-195062';
$bc = '654321';
Result : Error Cannot identify item for action! \\ wrong cat_id
example 3:
$cat_id = '';
$ds = 'ds-195062';
$bc = '654321';
Result : Error Cannot identify item for action! \\ no cat id
example 4:
$cat_id = '5';
$ds = '';
$bc = '654321';
Result : OK two out three aint bad as long as one is cat
\\ have a bc part number and a catagory
example 5:
$cat_id = '5';
$ds = 'ds-195062';
$bc = '654321';
Result : OK two out three aint bad as long as one is cat
\\ have a bc and ds part number and a catagory
example 6:
$cat_id = '5';
$ds = 'ds-195062';
$bc = '';
Result : OK two out three aint bad as long as one is cat
\\ have a ds part number and a catagory
This is an edit to the original question hoping it is more clear now
Thank You for looking
Ended up with this Thanks #trincot for explaining this to me
if ($_GET['act'] == 'del') {
$cat_id = ( isset( $_GET['cat_id'] ) && is_numeric( $_GET['cat_id'] ) ) ? intval( $_GET['cat_id'] ) : 0;
$ds = isset($_GET['ds']) ? $_GET['ds'] : '';
$bc = isset($_GET['bc']) ? $_GET['bc'] : '';
if ( $ds == '' ) { $ds = 'n'; }
if ( $bc == '' ) { $bc = 'n'; }
if($cat_id == 0 || ($ds == 'n' && $bc == 'n')) {
echo 'Error! ';
echo 'Result: value of ds ' . $ds .' value of bc '. $bc .' cat id is '. $cat_id;
}
else {
echo 'Good To Go ';
echo 'Result: value of ds ' . $ds .' value of bc '. $bc .' cat id is '. $cat_id;
}
}
You have the && and || operators in the wrong sense. Change this:
if($cat_id == '' && $ds == '' || $bc == '') {
echo 'Error Cannot identify item for action!';
}
to this:
if($cat_id == '' || ($ds == '' && $bc == '')) {
echo 'Error Cannot identify item for action!';
}
Note that the extra parentheses are not needed because && has precedence over ||, but it does not hurt to be clear.
Also, unless your else block contains all other code, you need to exit the script execution when this error occurs (maybe after doing some other handling first):
if($cat_id == '' || ($ds == '' && $bc == '')) {
echo 'Error Cannot identify item for action!';
// Some other handling/rendering could come here, but then exit:
exit();
}
Now, the variables will not be '' when the parameters are not passed at all. It would be good to cover for that as well (edit: and the numeric check which you had only in the second code block), and first do:
$cat_id = (isset($_GET['cat_id']) && is_numeric($cat_id)) ? $_GET['cat_id'] : '';
$ds = isset($_GET['ds']) ? $_GET['ds'] : '';
$bc = isset($_GET['bc']) ? $_GET['bc'] : '';
// ... and then:
if($cat_id == '' || ($ds == '' && $bc == '')) {
echo 'Error Cannot identify item for action!';
// Some other handling/rendering could come here, but then exit:
exit();
}
Just out of curiosity (and a bit of necessity):
if(! is_null($var)){
//do something
}
Is the above statement the same as
if($var != NULL){
//do something
}
No they are not the same.
The is_null function compairs the type also.
Example:
var_dump(is_null(0)); // bool(false)
var_dump(0 == NULL); // bool(true)
var_dump(0 === NULL); // bool(false)
So in your case
if(! is_null($var)){
//do something
}
Would be the same as
if($var !== NULL){
//do something
}
Yes this is (almost) correct, you can test this yourself:
$emptyvar1 = null;
$emptyvar2="";
if(is_null($emptyvar1) && $emptyvar1 == NULL){
echo "1";
}
if(is_null($emptyvar2)){
echo "2";
}
if($emptyvar2 == null){
echo "3";
}
if($emptyvar2 === null){
echo "4";
}
This will print 1 and 3.
because an empty string is equal to null if you only use 2 times =
if you use 3 times = it aint.
=== also checks object type
== only checks value
I'm not sure what exactly you're testing, but on:
a) $var = NULL;
neither of the statements triggers,
b) $var = 0;
is_null triggers and
c) $var = ''; is_null triggers aswell.
So the statements above are definitely not coming to the same conclusion.
See for yourself:
echo 'testing NULL case<br>';
$var = NULL;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing 0 case<br>';
$var = 0;
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
echo 'testing empty string case<br>';
$var = '';
if(! is_null($var)){
echo 'var is_null<br>';
}
if($var != NULL){
echo 'var != null<br>';
}
this outputs
testing NULL case
testing 0 case
var is_null
testing empty string case
var is_null
$start = true;
$new = "";
foreach($array as $val)
{
if($start = true && $val != " ")
{
$start = false;
$new .= strtoupper($val);
}
elseif($val == " ")
{
$new .= " ";
$start = true;
}
else
{
$new .= strtolower($val);
}
$start = false;
}
Basically what happens is $start NEVER becomes false AND everything becomes capitalized. So it looks like the first if IS running, but for some reason NEVER SETS $start to false.
I cannot stress it enough: use the yoda condition
if(true == $var)
or generally:
if(CONSTANT == $VARIABLE)
and not
if($VARIABLE == CONSTANT) //which you'd wrongly type as "="
PHP would have told you what went wrong in that case - no matter how tired you are.
Looking for this bug (it happens to the best of the best too) is frustrating.
Let the tool (PHP) be supportive to you, don't make it work against you.
That was on a more general note. As for your problem, it's doable with a one-liner:
<?php
$array = "hEllo woRlD";
var_dump(ucwords(strtolower($array)));
$start = true is an assignment, not a comparison. Use ==.
You're using a single equals in your test, which means "assignment". You probably meant == (equality), but in this case, with booleans, you don't need to compare at all:
$start = true;
$new = "";
foreach($array as $val)
{
if($start && $val != " ") // <-- remove the = true here
{
$start = false;
$new .= strtoupper($val);
}
elseif($val == " ")
{
$new .= " ";
$start = true;
}
else
{
$new .= strtolower($val);
}
$start = false;
}
Right now, it's getting interpreted as "Set $start to true && $val != " "" - definitely not what you intended.
$start = true && $val != " " means:
$start = (true && $val != " ")
$start = ($val != " ")
$start = !($val == " ") // $start is true except when $val is a single space.
I believe you meant $start == true.
Are you just trying to upper case the first character of every word? If so, look into ucwords.
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.