I have my variable, $mustcontain and I have the following:
if ($getvideoviews >= $minimumvideoviews
AND $getvideolikes >= $minimumvideolikes
AND $VidDuration >= $minvidlenght
AND $getchannelsubscribers >= $minsubs)
What I need is to check if $videotitle contains what´s in $mustcontain and if true then proceed to show some more information
You can use strpos() or in_array():
If $mustcontain is a string:
if ($getvideoviews >= $minimumvideoviews
&& $getvideolikes >= $minimumvideolikes
&& $VidDuration >= $minvidlenght
&& $getchannelsubscribers >= $minsubs
&& strpos($videotitle, $mustcontain) !== false) {
// do sth. with it
}
If $mustcontain is an array:
if ($getvideoviews >= $minimumvideoviews
&& $getvideolikes >= $minimumvideolikes
&& $VidDuration >= $minvidlenght
&& $getchannelsubscribers >= $minsubs
&& in_array($videotitle, $mustcontain) !== false) {
// do sth. with it
}
Related
I am setting some vars from GET
$start = $_GET['start'];
$end = $_GET['end'];
From which I get:
start=1-11-2018&end=30-11-2018
And then I am doing:
if((!$start) && (!$end)) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "no dates";
}
And to close it
if((!$start) && (!$end)) {
}
}
But this isn't happening
if((!$start) && (!$end)) {
UPDATE
Now this is working but it doesn't go in else if no GET
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "No dates";
}
Check via isset()
if you are calling : http://example.com?start=1-11-2018&end=30-11-2018
1. The isset() is checking query string "start"/"end" is having or not.
2. The empty() is checking query string "start"/"end" is empty/blank or not
if( isset($_GET['start']) && isset($_GET['end']) ){ // check the GET method is set or not
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
else {
echo "Empty dates";
}
}
else{
echo "Start / End date query string is missing...";
}
This is how I resolved it:
if((!empty($start)) && (!empty($end))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
// here the content which
// in case those vars are not empty,
// would get filtered by that logic.
if((empty($start)) && (empty($end))) {
// Close the condition for second if when not empty
} else {
echo "No dates";
}
In the project I am working, I get an age like
1Year 2Months 3Weeks 4Days
I am trying to do a comparison like
If age is less than 8 weeks, then it is a baby
If age less than or equal to 12 months/1 year then it is young
If greater than 1 year, it is adult. The code which I try is given below. It is confusing.
Is there a way to simplify this using some in built functions?
function getAgeGroup($age_years, $age_months, $age_weeks, $age_days) {
$age_group = 'baby';
if (!$age_years && !$age_months && (!$age_weeks || ($age_weeks <= 8 && (!$age_days || $age_days < 1)))) {
$age_group = 'baby';
}
elseif (!$age_years && ($age_months == 1 || $age_months == 2) && (!$age_weeks && !$age_days)) {
$age_group = 'baby';
}
elseif (!$age_years && $age_months == 1 && $age_weeks < 4 && $age_days) {
$age_group = 'young';
}
elseif (!$age_years && $age_months == 1 && $age_weeks > 4) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months >= 2 && $age_months < 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 0 && $age_months <= 12) && ($age_weeks >= 8 && $age_days > 1)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 2 && $age_months <= 12) && (!$age_weeks && !$age_days)) {
$age_group = 'young';
}
elseif ($age_years == 1 && !$age_months && !$age_weeks && !$age_days) {
$age_group = 'young';
}
elseif (($age_years > 0 || $age_months >= 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'adult';
}
elseif (($age_years > 0 || $age_months > 0)) {
$age_group = 'adult';
}
elseif ($age_years > 1) {
$age_group = 'adult';
}
return $age_group;
}
you can try to something like this: eval.in to get use of the DateTime and DateInterval classes.
I cannot find an answer to this and I am sure it is right in front of me. How do I say in PHP as part of an IF statement the following:
if NOT ( (variable1 == 10) && (variable2 == 20) )
Thanks!
You can use ! to achieve this
if ( (variable1 != 10) || (variable2 != 20) )
Or simply
if (!((variable1 == 10) && (variable2 == 20)))
if NOT ( (variable1 == 10) && (variable2 == 20) )
==>
if (! ( (variable1 == 10) && (variable2 == 20) ) )
<==>
if ( (variable1 != 10) ||(variable2 != 20) )
! mean not
so U can use it like this
if (! ( ($variable1 == 10) && ($variable2 == 20) ) )
or
if ( ($variable1 != 10) ||($variable2 != 20) )
&& will be || because !&& = ||
and U can't use variable1 without $ if U don't define it like this
define('variable1','value of variable1');
but then U can't change it's value so if statement will always have the same result and for that U should use $variable1
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";
}
?>
I'm creating a custom search form and when I try and sort the results I get all the objects displayed instead of the matched criteria. The reason I discovered was that some of the inputs from the form don't have a default value and when this is not declared in the conditional statement later on (for sorting) it just shows all the objects, whether the other requirements are met or not. I tried applying an OR statement with the specific variables able to be empty, but it gave the same result. Like so -
<?php if ($bedrooms >= $min_rooms
&& $bedrooms <= $max_rooms
&& $space >= $min_space
&& $space <= $max_space
&& $price >= $min_price
&& $price <= $max_price
&& $sel_type == $type
|| $sel_type == ''
&& $country == $sel_country
|| $sel_country == '' ) { ?>
(See the last two statements)
I was thinking of checking each variable in the conditional statement before including it but it feels like unnecessary code. How would you do it?
The && operator has a higher precedence than the || operator, so your expression is currently grouped like this, probably not what you want:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && $sel_type == $type)
||
($sel_type == '' && $country == $sel_country)
||
($sel_country == '' )
Try adding parentheses like this to achieve correct grouping:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '') )
Your expression may fail as the && operator has a higher precedence than the || operation. That means an expression like this:
… && $sel_type == $type || $sel_type == ''
is equivalent to this (operator precedence highlighted by using parentheses):
(… && $sel_type == $type) || $sel_type == ''
To fix that put the || expressions in parentheses:
$bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')
Additionally, your expression is probably easier to read and to maintain if you use some helper functions like a between function:
function between($val, $min, $max) {
return $min <= $val && $val <= $max;
}
Then your expression reads:
between($bedrooms, $min_rooms, $max_rooms) && between($space, $min_space, $max_space) && between($price, $min_price, $max_price) && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')