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
}
Related
I have 4 variables.
How can I do the following?
$x1='on';
$x2='';
$x3='';
$x4='';
if $x1=='on' and $x2 is empty and $x3 is empty and $x4 is empty , do this else do that ?
Like so:
if($x =='on' && $x2 =="" && $x3 == "" && $x4 == ""){
}else{
}
For future references PHP manual and here's link to expressive operators:
http://php.net/manual/en/language.operators.logical.php
Or, you can use the empty command, like so:
if($x =='on' && empty($x2) && empty($x3) && empty($x4)){
}
else{
}
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 ) { ...
I have conditional PHP working fine, the condition is if the url ends with url-1:
if ($currentpage == '/url-1') {
How can I change this so the url can be either url-1 or url-2? I cant get the syntax right.
Thanks
if ($currentpage == '/url-1' || $currentpage == '/url-2')
You need the || (logical OR) operator. More information on logical operators.
You can use it like so:
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
Be sure not to use:
if ($currentpage == '/url-1' || '/url-2') {
as this is valid but does not do what you would expect it to do.
Fella...that was hard to believe....
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
Have you tried REGEX?
$pattern = '/^\/url-[12]+/';
if (preg_match($pattern, $currentpage, $matches, PREG_OFFSET_CAPTURE)) {
}
That way, if you have lots of pages (1,2,...), you don't need a special case for each.
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
try
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
im not sure this is a good question to post but here is my issue. I have an if statement that is getting way too long and i was wondering if there is some other kind of syntax to shorten it out:
if (($time1 <= $one_day)&&
($time2 <= $one_day)&&
($time3 <= $one_day)&&
($time4 <= $one_day)&&
($time5 <= $one_day)&&
($time1 != NULL)&&
($time2 != NULL)&&
($time3 != NULL)&&
($time4 != NULL)&&
($time5 != NULL)){
//do sometihng
}
this is one example but i have a similar one that goes up to ..&&($time15 <= $one_day).
the statement is pretty self explanatory, $time1, $time2, etc can come back empty so i have to check if they are NULL or not
any ideas?
thanks
You can put the common stuff in a function:
function validate_time($time, $one_day) {
return $time <= $one_day && $time != NULL;
}
if (validate_time($time1, $one_day) &&
validate_time($time2, $one_day) &&
validate_time($time3, $one_day) &&
validate_time($time4, $one_day) &&
validate_time($time5, $one_day)) {
// do something
}
You may want to refactor code and eliminate the need for copying & pasting those checks. Another way to get the job done:
while (true) {
foreach (array($time1, $time2, $time3, $time4, $time5) as $time) {
if ($time > $one_day || $time == NULL) {
break 2;
}
}
// do something
break;
}
The above could be put in a function as well which would make the while-loop and break keyword redundant. Replace break 2 by return then.
Using an array for you variables would help. The you can iterate over them and check.
Put the times in an array and have a for loop to do the checking.
Instead of using 15 similar but different variables, consider using an array.
If you must (or want to) keep the original variable names and not use an array here is the good solution (for $time1 to $time5):
$ok = true;
for ($i = 1; $i <= 5; $i++)
{
$var =& ${'time'.$i};
if ( ! ($var <= $one_day && $var != NULL))
{
$ok = false;
}
}
if ($ok)
{
//do something
}
You can set all the values into an Array and compare it using an For Loop.
A functionised version which should help with your reuse. This is similar to Lekensteyns code.
$times = array(
'time',
'time',
'time',
'time',
'time',
);
function validateTime($checks, $limit)
{
foreach($checks as $check) {
if($check == null || $check > $limit) {
return false;
}
}
return true;
}
if(validateTime($times,$one_day) == true) {
//codey code.
}
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.