How to write long IF more prettier? - php

I have long IF:
if(rand(1, 100) == 22 && $smth < time() &&
$smths > 5 && $sxsxsx > 250 &&
!$_SESSION['false'])
{
echo "wow, big if just happened!";
}
How to write it more "prettier"?

I prefer breaking before the boolean operators.
if(rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false']
)

I like to name my conditions and group them so its clear what their purpose is.
$is22 = rand(1, 100) == 22;
$someTime = $smth < time() && $smths > 5;
$meetsSx = $sxsxsx > 250;
$inSession = !$_SESSION['false'];
if ($is22 && $someTime && $meetsSx && $inSession) {
// do something
}

$isSomethingValid = rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false'];
if ($isSometingValid) {
// do something
}

In accordance with my answer to the related
Multiple condition IF statement
this should be refactored with Decompose Conditional, which means you should make the individual tests into separate functions. And you should get rid of the magic numbers and meaningless variable names. I would give you an example on how to do that for your code, but the code is incomprehensible.

Always indent to the enclosing statement one extra than the body of the block. You would write a function like this:
function (reallylongparam, reallylongparam, reallylongparam,
reallylongparam, reallylongparam) {
doStuff()
}
so you'd write your if statement like this:
if(rand(1, 100) == 22 && $smth < time() && $smths > 5
&& $sxsxsx > 250 && !$_SESSION['false']) {
doStuff();
}

probably
if(
rand(1, 100) == 22 &&
$smth < time() &&
$smths > 5 &&
$sxsxsx > 250 &&
!$_SESSION['false']
) {
echo "wow, big if just happened!";
}
cheers

Making your code readable is a very important aspect when it comes to supporting your code - someone else might have to do that support.
Have a look at coding styles (search around for more info if you must).
Personally I would format that snippet like so:
if (
rand(1, 100) == 22
&&
$smth < time()
&&
$smths > 5
&&
$sxsxsx > 250
&&
!$_SESSION['false']
)
{
echo "wow, big if just happened!";
}

Just encapsulate the boolean logic in a seperate function

You could also make your variable names easier to read.

Related

Multiple conditionals in php

if
(
($page_num >=10 && $page_num<= 20) ||
($page_num >= 21 && $page_num =< 30) ||
($page_num >=31 && $page_num =<40)
){
if($i==1){
$i = $page_num;
}
}
I am trying to achieve multiple conditionals using the above in PHP. I tried it and it kept outputting an error, so I don't know if I am making a mistake or what I am trying above is not possible in PHP or programming.
The error message
Parse error: syntax error, unexpected '<' in C:addsdcredit.php on line 398
You have =< on your code, change them like this: <= .
check http://php.net/manual/fa/language.operators.precedence.php.
Hope it helps.
$page_num = 11;
$i=1;
if(($page_num >=10 && $page_num<= 20) || ($page_num >= 21 && $page_num <= 30) || ($page_num >=31 && $page_num <=40)){
if($i==1){
$i = $page_num;
}
}

check if 3 number are odd with PHP

I'm trying to check if these 3 numbers are odd:
if ( ($pezzi % 1) && ($base % 1) && ($altezza % 1) ) {
}
Why my script don't work? I do not understand why..
if ( ($pezzi % 2 !=0) and ($base % 2 !=0) and ($altezza % 2 !=0) ) {
}
Try this way:
if((number1 % 2) != 0 AND (number2 % 2) != 0 AND (number3 % 2) != 0){
-- do something
}
You should do like this:
if ($pezzi%2!=0 && $base%2!=0 && $altezza%2!=0) {
}
if ( ($pezzi & 1) && ($base & 1) && ($altezza & 1) ) {
}
Your statement is true. You just didn't compare it with 0:
if(($pezzi % 2) != 0 && ($base % 2) != 0 && ($altezza % 2) != 0){
}

PHP "if" statement looking for between two numbers

I need help on a if statement here is what is going on.
I have a value being pulled $paint['product_id']
I need to say if that value is between
81501 - 81599 or
81701 - 81799
say blah
else if that value is between
81001 - 81099 or
81301 - 81399
say blah2
else if
86501 - 86599 or
86001 - 86099 or
85001 - 85099
say blah3
and say nothing if it does not apply.
What id did try
<? if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799):?>
blah
<? elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399):?>
blah2
<? elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099):?>
blah3
<? endif;?>
The problem I am having is "blah" is showing up on items in the blah3 category.
Hope that makes sense and thanks in advance for any help!
Replace $x with $paint['product_id'].
You should group them with brackets:
if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...
Consider creating a user defined function, e.g.
function between($x, $lim1, $lim2) {
if ($lim1 < $lim2) {
$lower = $lim1; $upper = $lim2;
}
else {
$lower = $lim2; $upper = $lim1;
}
return (($x >= $lower) && ($x <= $upper));
}
Then the rest of your code becomes much more legible:
if between($paint['product_id'], 81501, 81599) blah;
As given, the "between" function will work even if you don't know ahead of time whether the first or the second argument is larger.
You need more parenthesis
<?php
<? if (($paint['product_id'] >= 81501 && $x <= 81599) || ($paint['product_id'] >= 81701 && $x <= 81799)):?>
blah
<? elseif (($paint['product_id'] >= 81001 && $x <= 81099) || ($paint['product_id'] >= 81301 && $x <= 81399)):?>
blah2
<? elseif (($paint['product_id'] >= 86501 && $x <= 86599) || ($paint['product_id'] >= 86001 && $x <= 86099) || ($paint['product_id'] >= 85001 && $x <= 85099)):?>
blah3
<? endif;?>
there are at least 3 ways to solve it.
First solution has been already posted by users
Second solution is to create between function and use it.
function between($number, $from, $to)
{
return $number>$from && $number<$to;
}
if(between($paint['product_id'], 81501, 81599) || $paint['product_id'], 81701, 81799))
echo 'blah';
else if(between($paint['product_id'], 81001, 81099) || $paint['product_id'], 81301, 81399))
echo 'blah2';
else if(between($paint['product_id'], 86501, 86599) || $paint['product_id'], 86001, 86099) || $paint['product_id'], 85001, 85099))
echo 'blah3';
else echo 'it does not apply';
Third solution is to use range() function and in_array() function
example if(in_array($paint['product_id'], range(81501, 81599)))
rest goes the same
if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799){
echo "blah";
}
elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399){
echo "blah2";
}
elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099){
echo "blah2";
}
Hellow i'm going to desribe how to use if condition between two numbers in PHP in easy steps.
<?php
$a= 65;
$b= 9;
if($a<$b)
{
echo "$a this is correct";
}
else{
echo "$b this is greater than $a";
}
?>

Php code for is not in array

I need a little bit of help, being a newb with php.
if($catname != 'used-cars' && $currentpage IS NOT 1,2,3.....100){
}
How can I write this corectly?
Maybe put the numbers inside an array?
Ty
Use ! and in_array()
$array = array(1, 2, 3... , 100);
if($catname != 'used-cars' && !in_array($currentpage, $array)){
}
http://php.net/manual/en/function.in-array.php
if($catname != 'used-cars' && !in_array($currentpage, range(1, 100))
Or:
if($catname != 'used-cars' && ($currentpage < 1 || $currentpage > 100))
if($catname != 'used-cars' && !in_array($currentPage, array(1, 2, 3, ..., 100)))
{}
Assuming you want all numbers withing the range of 1-100, you can use in_array() and range() like so:
if (($catname != "used-cars") && (!in_array($currentPage, range(1,100))) {
//Do Stuff
}
Maybe something like this might work?
if($catname != 'used-cars' && $currentpage > 100)
{
//Code here
}

How to use operators so that they exclude zero?

In my database I have "maximum files" but I would like to set it at 0 for unlimited.
Therefore I need a way to
if ($count >= $max){
//a value of 0 in $max should not be here
} else {
//but here
}
Is this possible or do I have to create an exclusion for 0?
if($max && ($count >= $max)) ....
if (($max !== 0) && ($count >= $max) ){
if (!empty($max) && $count >= $max ) { ...

Categories