if statement formatting - php

Basically, I always use words 'and' & 'or' so, my scripts look like:
if ($value == '1' or $value == '2' or $value == '3') {
//do stuff
}
if ($value == '1' and $user_logged == 'Admin') {
//do stuff
}
It's possible get rid of of $value repeating?
Any benefit in using and and or instead of || and && ?

There is no way to shortcut expressions like
if ($value == 1 or 2 or 3) // invalid
But you can check the values in another way:
if (in_array($value, array(1, 2, 3))) {
// do stuff
}
The difference between or and || is operator precedence. Use || and &&, not or and and.

Answer 1.
$inArray = (1,2,3);
if (in_array($value,$inArray)) {
// do stuff
}
Answer 2.
Operator precedence

You can use first if as a switch statement:
switch($value) {
case "1":
echo "value is 1";
break;
case "2":
echo "value is 2";
break;
case "3":
echo "value is 3";
break;
}
Answer to your second question: Operator precedence

Related

Can't get array_push working

I want to add values to an array so the output will be like this (1,2,3,4,5).
To accomplish this i tried to use array_push. I've tried a lot to get this working but it just outputs the last number which in this case is '5'. Do you guys see what is wrong with my code? Thanks in advance!
$websites = array();
case 'Hengelsport':
if ($waarde == 'true') {
array_push($websites,1);}
break;
case 'Diervoeders':
if ($waarde == 'true') {
array_push($websites,2);}
break;
case 'Vijverconcurrent':
if ($waarde == 'true') {
array_push($websites,3);}
break;
case 'Broqx':
if ($waarde == 'true') {
array_push($websites,4);}
break;
case 'Dekrabpaal':
if ($waarde == 'true') {
array_push($websites,5);}
break;
$this->articleData['website_ids'] = $websites;
You can't to have other value that last number, if you juste set $websites variable, like this : $websites = array();
And don't forget switch operator.
switch ($i) {
case "Hengelsport":
echo "i égal 0";
if ($waarde == 'true') {
array_push($websites,1);
}
break;
//...
}
Is it full code that you show us ?
Or for an easy way :
$website_ids = array(
'Hengelsport' => 1,
'Diervoeders' => 2,
'Vijverconcurrent' => 3,
'Broqx' => 4,
'Dekrabpaal' => 5
);
if ($waarde == 'true')
{
array_push($websites, $website_ids[$your_var_containing_name]);
}

Best way to address several type of users in PHP/MySQL

I'm looking for something like this:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
?>
I have some 7 conditions and dozens of combinations. I want to know is this the right way to address each unique type of user or there can be some better approach?
HEREDOCS for every single combination is totally different from others.
A switch with concatenated key may be the answer for you:
switch ($condition1.":".$condition2.":".$condition3){
case "a:a:a":
//echoing some HEREDOCS here
break;
case "a:a:b":
//echoing some HEREDOCS here
break;
case "a:b:a":
//echoing some HEREDOCS here
break;
case "a:b:b":
//echoing some HEREDOCS here
break;
}
Or alternatively you could create a message map
$messages = array(
"a:a:a"=>"Docs a:a:a",
"a:a:b"=>"Docs a:a:b",
"a:b:a"=>"Docs a:b:a",
"a:b:b"=>"Docs a:b:b",
);
echo $messages[$condition1.":".$condition2.":".$condition3];
You might be looking for the switch operator.
As far as your design is concerned, you might want to have each persons text stored in the database and pull it up with the user!
I always like to design my code to avoid this kind of situation. Try to define a linear list of situations first, like:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a') $user_type = 1;
elseif($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b') $user_type = 2;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a') $user_type = 3;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b') $user_type = 4;
?>
Just don't forget to cover every possible combination, otherwise all ifs may fail, making $user_type undefined.
Later, when you want to print something, or take any action based on user type, just do a switch / case:
switch($user_type)
{
case 1:
//echoing some HEREDOCS here
break;
case 2:
//echoing some HEREDOCS here
break;
}
The general idea is to handle the complicated thing only once, to avoid duplicating your complex logic all around your code.

Best way to check what a variable is

I was wondering what the quickest way would be to do something like the following:
if ($var == 1) {
// 1
}
if ($var == 2) {
// 2
}
if ($var == 3) {
// 3
}
etc, but then at the end having something like:
if ($var != 1 or 2 or 3) {
//Not a number
}
I was thinking about having an if(in_array(...)) statement at the end, but wanted to know your thoughts.
I would do this with a switch
switch ($var) {
case 0:
echo "var equals 0";
break;
case 1:
echo "var equals 1";
break;
case 2:
echo "var equals 2";
break;
default:
echo "var is not 0 1 or 2"
}
Also if you miss out a break statement then you can easily do a case when $var == 1 || $var == 2, read more
If all you want to know is whether "$var" is in your set {1, 2, 3}, then in_array is fine.
Otherwise, if you want to know which (if any) value you've got, then I'd do this:
if ($var == 1) {
// 1
}
else if ($var == 2) {
// 2
}
else if ($var == 3) {
// 3
}
else {
}
Note the "else if" to save you from re-checking what you already know.
Note, too, that PHP 4 and 5 also have a "switch" case/block:
switch ($i) {
case 1:
// 1
break;
case 2:
// 2
break;
case 3:
// 3
break;
default:
...
}
Or use a switch case, this better to read for much cases:
switch($var) {
case 1: /*1*/ break;
case 2: /*2*/ break;
case 3: /*3*/ break;
default: /*not 1 not 2 not 3*/
}
Use if/else if/else, with which you can decide whether strict or loose comparisons are appropriate.
With switch, it's always loose comparisons, which is why I prefer the if statement, since it makes it explicit what mode has been chosen.
if ($var === 1) {
// 1
}
else if ($var === 2) {
// 2
}
else if ($var === 3) {
// 3
}
else {
//neither 1, 2 nor 3
}

if statement with multiple condition in php

I have if statement with multiple condition,
what is the differece between this two conditon:
1.
if($province=="AB" || "NT" || "NU" || "YT")
{
$GST=5;
}
else if($province=="BC" || "MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || "NF" || "ON")
{
$HST=13;
}
and second is:
2.
if($province=="AB" || $province=="NT" || $province=="NU" || $province=="YT")
{
$GST=5;
}
else if($province=="BC" || $province=="MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || $province=="NF" || $province=="ON")
{
$HST=13;
}
The difference between the two is that the first one won't work as expected, and the second is technically correct.
The code:
if($province=="AB" || "NT" || "NU" || "YT")
will always evaluate to true and execute the code in that conditional block.
The reason is because you are only checking if $province == "AB" and then you are checking if "NT" == true which will evaluate to true.
To check province against all of those values (AB, NT, NU, YT) you need to explicitly check $province against each value, not just the first, which is what you are correctly doing in the second example.
I add to drew010 answer that you can do this too (wich is simpler) :
if(in_array($province,array("AB","NT","NU","YT"))
{
$GST=5;
}
else if(in_array($province,array("BC","MB"))
{
$GST=5;
$PST=7;
}
else if(in_array($province,array("NB","NF","ON"))
{
$HST=13;
}
The first one will always evaluate to true. In the second, third, and forth OR clauses in the first example you are basically asking PHP to convert the strings to Boolean values, and non-empty strings will always evaluate as true.
Just for fun, my favorite way to handle conditionals like this is with a switch statement. It's a lot easier for me to read, but it's really a personal preference thing.
switch ( $province ) {
case 'AB' :
case 'NT' :
case 'NU' :
case 'YT' :
$GST = 5;
break;
case 'BC' :
case 'MB' :
$GST = 5;
$PST = 7;
break;
case 'NB' :
case 'NF' :
case 'ON' :
$HST = 13;
break;
}

Multiple comparison operators for one variable?

I need to do multiple checks for a variable. I've seen an "Equals" example, here: w3schools.
But they are two different variables. Right now I have:
if ($color == 'blue')
{
//do something
}
But I need to to multiple checks for $color. Eg if it equals red or green too. How is this written?
As simple as:
if ($color == 'blue' || $color == 'red' || $color == 'green') {
//do something
}
There are several other options. Using switch operator:
switch ($color) {
case 'blue':
case 'red':
case 'green':
//do something
}
Or more complex using in_array function:
$colors = array('blue', 'red', 'green');
if (in_array($color, $colors)) {
//do something
}
Use a switch-statement.
switch($color)
{
case "blue":
// do blue stuff
break;
case "yellow":
// do yellow stuff
break;
case "red":
// do red stuff
break;
default:
// if everything else fails...
}
In case you want to do the same thing on all colors, just use the || (boolean or) operator.
if ($color == "blue" || $color == "red" || $color == "yellow")
{
// do stuff
}
You can also go with preg_match on this one. This might be overkill, but I'm sure it's very fast!
$color = "blue";
$pattern = "/^red|blue|yellow$/";
if ( preg_match($pattern,$color) ) {
// Do something nice here!
}

Categories