Firstly, dont laugh, I know it is basic.
I have this code: if ($pageid == '9') { echo... and I want to add an OR to it, so I tried: if ($pageid == '9' or '5') { echo... but it didnt work. How do I add an OR function to this?
if ($pageid == '9' || $pageid == '5') { echo...
Note that if you have a large number of pageids to check for some reason, you can utilize arrays:
if(in_array($pageid, array(9, 5, 3, 12, 5)) { echo ...
or works instead of || also but || is more commonly used.
Just to mention another approach that makes use of PHP's in_array - it's quite handy if you've a few values you want to treat as a cluster that don't justify a switch but would be painfully verbose to use a traditional or for.
if(in_array($pageID, array(1, 2, 3))) {
// Do exciting things...
}
if ($pageid == '9' || $pageid == '5')
this should work too. i am not sure why this won't work in your case
if($pageid == "9" OR $pageid == "5"){
echo "pageid ".$pageid;
}
for more information see this http://php.net/manual/en/language.operators.logical.php
switch ($pageid) {
case '5':
case '9':
echo "...";
break;
default:
// Do something useful
break;
}
This is useful, if you have more than one cases, because it prevents you from ugly "elseif"-chains. Also if you have one case with many different values its more readable and its easy to add additional values. You just need to add a new case 'xy': line.
Related
Is there a shorter way of writing this?
<?
if($_GET['id']==1 ||
$_GET['id']==3 ||
$_GET['id']==4 ||
$_GET['id']==5)
{echo 'does it really have to be this explicit?'};
?>
Something like this perhaps?
<?
if($_GET['id']==1 || 3 || 4 || 5){echo 'this is much shorter'};
?>
Just try with:
if ( in_array($_GET['id'], array(1, 3, 4, 5)) ) {}
Maybe not shorter but more readable. Try the in_array() function:
if (in_array($_GET['id'], array(1, 3, 4, 5)))
{
echo "What about this?";
}
Perhaps switch may help
switch($_GET['id']) {
case 1:
case 3:
case 4:
case 5:
echo 'Slect maybe :P';
break;
}
You can use regular expression like below:
preg_match(['1-4']);
Declare an array:
$values = array(1,3,4,5);
Get your variable
$id = $_GET['id'];
Now use PHP in_array();
if(in_array($id, $values)){
//do something
}
Read about in_array()
I have some code that isn't working yet, before I debug I want to make sure that this syntax or method can indeed work and actually only execute the mysql_query if the last condition is true.
Also, is this a relatively safe practice?
I couldn't find anything relating to this, I figured someone putting it in English would help clear this up for me.
if($var1 == $var2) {$new = 1;}
if($vara == $varb) {$old = 1;}
if($new = 1 && $old = 1) { mysqli_query($somequery);}
This won't work because of the single =.
Go for:
if($var1 == $var2) {$new = 1;}
if($vara == $varb) {$old = 1;}
if($new == 1 && $old == 1) { mysqli_query($somequery);}
Or, ideally:
if ($var1 == $var2 && $vara == $varb) {
mysqli_query($somequery);
}
Top hint to stop things like if ($var = 1) typos - switch the comparisons around and put the constant first.
If you write if ($var = 1) then $var becomes 1 and is always true, but if you write if (1 = $var) you get an error, which is exactly what you want (and the same happens if your use a string if ("yes" = $var).
It been hammered into us to put the variable first since forever, but you're far better off doing it the other way around.
Im not great in php and I could do with a little help. I want to say something like
if ($x == 1 or 2 or 3 or 4) {do function}
but the only way i know how to do that is to go
if (($x == '1') or ($x == '2')) or...
which seems a long way of doing it. Is there a better way I am missing, like
if ($x == 1,2,3,4) {do}
Thanks for your answers!
you can use in_array function
$array = array(1,2,3,4)
if(in_array($x, $array)) {
// do something
}
switch ($x) {
case 1:
case 2:
case 3:
case 4:
// do
break;
}
Or you can use the in_array() function creating an array such as $a = array(1,2,3,4); and then do if (in_array($x, $a)).
If you are concerned about space, you can also use the shortcut:
if (in_array($x, array(1,2,3,4))) { /* do */ }
You can create an array of expected values and then use function in_array().
http://php.net/manual/en/function.in-array.php
If it's a range, you could do:
if ($x >= 1 && $x <= 4) { }
You could also construct an array and check if the number is in that array.
<?php
$data = array(1,2,3,4);
if(in_array($x, $data)){
// execute function
}
?>
All the above ideas are good. I am going to show another way, that is not better, but is different.
You can store comparations in variables, to use later or combine. This helps readability, and make complex expresions easy to create and read. It obviusly remove any repetition.
$is_number = ($str=="one" or $str=="two" or $str=="tree");
$is_english = ($str=="one" or $str=="horse");
$is_french = ($str=="baguette" or $str=="amie");
$is_fun = $is_french or $is_english;
if($is_french and !$is_number){ ... }
So I have a variable we will say $string = 2;
if i want to check that variable for either 2 conditions I'd typically do
if($string == 2 || $string == "Hello world"):
Is there anyway to combine these two args into something shorter like
if($string(==2 || =="hello world")):
I've googled this but I cant come up with the right phrase to get back the answer I'm looking for so explaining it at this point is the easiest.
if (in_array($string, array(2, 'Hello world')))
See http://php.net/in_array.
A sick part of me wanted to answer this.
function f($a){
$args = func_get_args();
return in_array($a, array_slice($args, 1));
}
used like
if(f($str, 1, "hello world", 3, "etc")){
}
If variable have many states that can be processed in different ways, you can use switch statement:
switch($var)
{
case 1:
case 2:
//some action
break;
case 3:
break;
}
Simple but this has always bothered me. Which is the best way to condition statement?
$foo = '1';
if($foo === '1' || $foo === '2' || $foo === '3')
{
// foo matches
}
or
if($foo === '1' || '2' || '3')
{
// foo matches
}
Which step works and is better. is there a better solution?
The second version will always evaluate to true.
If you want to compact the comparison against multiple alternatives then use:
if (in_array($foo, array('1', '2', '3') )) {
If you want to closely match the exact comparison === then you would however need:
if (is_string($foo) && in_array($foo, array(...))) {
$foo = 1;
if(in_array($foo, array(1, 2, 3))){
//foo matches
}
This is an alternative:
if($foo>0 && $foo<4){
}
Second if statement won't work. PHP doesn't work like that. Any number other than 0 (including negatives) evaluates to true when alone in an if statement. This is why you can do something like if(count($array)) without specifying that count($array) must be greater than 0.
Would be the same as if you had said:
if($foo === 1)
{}
elseif(2) //This will always trigger if $foo !== 1.
{}
elseif(3) //This will never trigger because of the last one
{}
Each condition is it's own self contained condition. Instead of reading it as just "or" or "and" read it as "or if" and "and if". So if $foo is 1 or if 2 or if 3 instead of if $foo is 1 or 2 or 3
If it's just numeric, then amosrivera's solution is the best. If it's for other types of data, then webarto/mario have a good solution.