Is this a valid syntax in php? I want a call to be done if these two conditions are met.
if($valuear[$i] < $rake) and ($winuser != $winar[$i])
Thanks.
No, it isn't
if (($valuear[$i] < $rake) && ($winuser != $winar[$i]) ){}
You must wrap the whole condition in brackets.
This could help you:
and : &&
or : ||
Set the brackets in this way:
if($valuear[$i] < $rake and $winuser != $winar[$i])
http://php.net/manual/en/control-structures.if.php
The basic syntax is:
if (expr)
statement
you will get syntax error for what you have done
if($valuear[$i] < $rake) and ($winuser != $winar[$i])
It should be
if(($valuear[$i] < $rake) and ($winuser != $winar[$i]))
^ ^
This is the valid way :
if($valuear[$i] < $rake && $winuser != $winar[$i])
Related
I am trying to create an IF function with two conditions however, I am getting parse error "can't use value return in parse context". Any ideas please?
$youlink = get_field('fl_tube');
if (strlen ($youlink) < 50 && substr($youlink, 0, 23) = "https://www.youtube.com"; ) {
some code here...
}
you have used the assignment operator = instead of the comparison operator ==. There is also an added ; after the string https://www.youtube.com which is a syntax error.
try the following code:
$youlink = get_field('fl_tube');
if (strlen ($youlink) < 50 && substr($youlink, 0, 23) == "https://www.youtube.com" ) {
some code here...
}
Hope this helps.
I'm absolutely confused with this:
$email = (isset($_POST['email']) && preg_match('#^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#', $_POST['email']) && strlen($_POST['email']) <= 255 && strlen($_POST['email']) > 6) ? $_POST['email'] : false;
Error:
Warning: preg_match(): Unknown modifier '[' in C:\xampp\htdocs\sales\qls-3.1.9\install\Install.class.php on line 283
and I've looked at the actual functions webpage from the official php site
If anyone can help that would be epic!
Problem is that you're using # as regex delimiter and using # in the regex without escaping it.
Your regex:
#^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$#
ok try this:
$email = (isset($_POST['email']) && preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_POST['email']) && strlen($_POST['email']) <= 255 && strlen($_POST['email']) > 6) ? $_POST['email'] : false;
Hi all i have a post value which i am checking to see if its been posted it has atleast 4 numbers (digits) this works perfect.
if (isset($_POST['year']) &&
!preg_match('/([0-9]{4})/i', stripslashes(trim($_POST['year']))) ) {
now i want to check that the value is greater or = to a vairable and not sure how to achive what i need
i tried the below with no luck
$yearOff = date("Y")-150;
echo $yearOff;
if (isset($_POST['year']) &&
!preg_match('/([0-9]{4})/i', stripslashes(trim($_POST['year'])))
&& $_POST['year'] > $yearOff ) {
$ageerrors[] = '<span class="error">
You forgot enter your birth YEAR</span>';
}
Rather than an && you need an || OR condition to switch between the three possible invalid states (empty, not 4+ digits, or <= $yearOff:
if (!isset($_POST['year'])
// Lose the stripslashes()...
|| !preg_match('/([0-9]{4})/i',trim($_POST['year']))
|| $_POST['year'] > $yearOff
) {
// Invalid...
}
Note: It isn't clear from your description whether you want the value to be >= $yearOff or you want it to be < $yearOff. In other words, the code above is testing for the invalid state. Use whichever operator is appropriate for the invalid state.
Note 2: To test for at least 4 consecutive digits in the regex, a better pattern is something like:
/\d{4,}/
// If it must be *only* digits and no other characters, anchor with ^$
/^\d{4,}$/
There's no need for the overhead of a () capture group.
$yearOff = date("Y")-150;
echo $yearOff;
$input = #$_POST['year'];
if (!$input || strlen($input) !== 4 || $input < $yearoff) {
### MEEEP, ERROR ###
}
Explanation:
Input is set (not null which would be false), then it must have a string-length of four and finally it's numerical value must be higher or equal $yearOff.
I assigned the value of the input to it's own variable as well, because you only need to take it once out of $_POST.
As all these conditions are negated, I used the or || operator. Naturally the same can be expressed non-negated and with and:
if ($input && strlen($input) === 4 && $input >= $yearoff) {
### THIS IS CALL OKAY ###
}
To better debug this, the next step is to assign the validity to a variable as well:
$inputValid = $input && strlen($input) === 4 && $input >= $yearoff;
if (false === $inputValid) [
### MEEP, ERROR ####
}
Hope this is helpful.
I don't know why, but this doesn't seem to work.
Basically I want this to be true if the player1 ($playerX, $playerY) is within one square from player2 ($rs[x], $rs[y])
if (($rs[x] > $playerX-2 or $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 or $rs[y] < $playerY+2)) {
// code
Any idea what I'm doing wrong?
If you replace with some test values, you might get:
if( (3 > 5 or 3 < 9) && (13 > 15 or 13 < 19))
Clearly, this is always true.
You should use && instead of or in this case.
It's just a case of replacing or with and. Here's a neater way of writing your if statement for bonus points:
if(abs($playerX - $rs[x]) < 2 && abs($playerY - $rs[y]) < 2) {
// code
Your conditions end up on true due to the OR operator
if (($rs[x] > $playerX-2 && $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 && $rs[y] < $playerY+2)) {
//your code
}
$n=21;
$p=$n%10==1 && $n%100!=11 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2;
why is $p = 2?
it is supposed to be $p = 0!
is it a bug or am I missing something?
I got this from trying to get the plural form for Russian on: http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html
should be this one:
$p=($n%10==1 && $n%100!=11) ? 0 : (($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20)) ? 1 : 2);
the error was in missing bracets
you can see here: http://php.net/manual/en/language.operators.comparison.php that "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious". You can see that if you enclose the else part for the first if between ( and ) you will get another result:
$p=$n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2);
Maybe you should consider changing you statement to a "regular" if block, something like:
if ($n%10==1 && $n%100!=11)
{
$p =0 ;
}
elseif ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20))
{
$p = 1;
}
else
{
$p= 2;
}
this way being easier to read