URL
x.php?foo=100
x.php
$x = $_GET['foo'];
if ($x = 100) {
echo "yeah";
}else{
echo "no";
}
My code doesnt work, where is error?
You are using a single equal mark (=) instead of two in the IF statement (if ($x = 100) {).
A single equal mark will set the value 100 into $x, and then evaluate the IF statement with it - which evaluates to true in PHP.
if ($x == "100")
or
if (intval($x) === 100)
if ($x = 100) {
sets $x to 100 and evaluates the result. You want:
if ($x == '100') {
It is because you are using an assignment statement instead of checking for equality in the line:
if ($x = 100)
Try
if ($x == 100)
instead.
you are not comparing value of x, you are saying that x is equal to 100
try
if($x == 100) { echo "yeah"; }
first check with isset($_REQUEST['foo']) or isset($_GET['foo'])
Related
I have a question about IF statements with multiple logical OR operators.
If we let:
$x=1;
A. I typical would write a IF statement comparing two items like this:
if($x == 1 || $x == 2) echo 'good';
else echo 'bad';
B. But, is this a valid IF statement? If not, why? (because it seems to work)
if($x == (1 || 2)) echo 'good';
else echo 'bad';
C. I typical would write a third comparison like this:
if($x == 1 || $x == 2 || $x == 3) echo 'good';
else echo 'bad';
D. What about this, following suit with B, above? (it does not seem to work)
if($x == (1 || 2 || 3)) echo 'good';
else echo 'bad';
The example in B, above works, but not the example in D. Why?
I cannot find any PHP documentation as to why.
Here is what happens for every version:
A. $x == 1 || $x == 2
PHP will compare $x with the value 1, this is true so it can short-circuit the if and echo 'good'.
B. $x == (1 || 2)
PHP will evaluate 1 || 2 because parentheses indicate the priority, as the result should be a boolean expression it will cast 1 to a boolean which evaluates to true so the expression becomes $x == true.
Now PHP will evaluate this expression. First it will cast both types to the same, according to the documentation, it will "Convert both sides to bool". So, same as above, as $x is 1 it will be cast to true and then the expression becomes true == true which is true.
C. $x == 1 || $x == 2 || $x == 3
It is the same as A.
D. $x == (1 || 2 || 3)
It is quite the same as B.
And, for the record 1 == (1 || 2 || 3) evaluates to true.
I am currently teaching myself web development/ programming and to learn php i have built a simple program. The program takes user input and based on a series of math algorithms and calculates 7 random lottery numbers. The code is working fine but i want to improve it. The code is very repetitive and i want to simplify it by creating my own functions. I have created the first function that takes the users input, simply does some maths and then returns some values.
For Example...
<?php
function some_maths($int1 $int2 $int3){
$x = $int1 + $int2;
$y = $int2 * $int3;
$z = $y * $x;
return $x
....}
So this is pretty straight forward, but what i want to do now is take the values of X, Y, Z and create a function that checks to make sure they're not matching, or that they're not less than 1 or greater than 59. I used a while loop in my original code that goes like this:
while($x == $y || $x == $z || $x <1 || $x >59){
if( x> 59 || x < 1){
if (x<1){
do{ $x+=$int}while($x <1);
}elseif ($x > 59){
do{ $x-=$int}while($x >59);
}else $x++;
}
This seems to work fine but i don't want to have to repeat the same code over and over. I am sure there has to be a better way? Could i put the values into an array and maybe do it that way? What would be the best solution for this?
Your question is kind of vague but if I had to write a function to check if three numbers weren't equal and were < 59 and >1 this is how I would do it
function validateNumbers($x , $y , $z)
{
if(equal($x,$y)) return false;
if(equal($x,$z)) return false;
if(equal($y,$z)) return false;
if($x>59||$x<1) return false;
if($y>59||$y<1) return false;
if($z>59||$z<1) return false;
return true;
}
function equal($x , $y)
{
if($x == $y)return true;
else return fasle;
}
So far I only see two (pretty straightforward) things:
Your function prototype in the first example is missing commas between the parameters. Instead of function some_maths($int1 $int2 $int3) it should read function some_maths($int1, $int2, $int3).
In your second example a closing } is missing. But if I am interpreting your stuff correctly, the outer if-clause is redundant. Thus, the snippet can be simplified to:
Second example:
while($x == $y || $x == $z || $x <1 || $x >59){
if (x<1){
do{ $x+=$int}while($x <1);
}
elseif ($x > 59){
do{ $x-=$int}while($x >59);
}
else $x++;
}
There may be more room for improvement (e.g. slim down the condition of the outer while loop) - but for that we would need more context (what happens before your loop, what is $int, ...).
This code works
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
but if instead of " <= " I use simply " = " php gets into an infinite loop that timesout
<?php
for ($x = 0; $x = 10; $x++) {
echo "The number is: $x <br>";
}
?>
Is that expected behavior?
Yes, you're using an assignment operator instead of a comparison operator. Now there's no way for that loop to end.
Yes, it's expected.
You must use <= instead of = only
<= is a comparaison operator : http://php.net/manual/en/language.operators.comparison.php
= is an assignment operator : http://php.net/manual/en/language.operators.assignment.php
How can I increment $x using the ternary operator?
I've tried $x = $x==2 ? 0 : ++; but obviously it didn't work.
if($x == 2 ){
$x=0;
}else{
$x++;
}
Thanks.
You'll want to use pre-increment here.
$x = ($x==2) ? 0 : ++$x;
Demo!
The best way to do this would be:
$x = $x === 2 ? 0 : $x+1;
If you insist on using a ternary just to increment the var:
$x += $x === 2 ? $x*(-1) : 1;//or hard-code -2 instead of $x*(-1)
This either adds 1 to $x or adds $x*-1 to $x ($x + (-$x) is 0). On the whole, though, I'd like to add that ternary (especially in PHP) should be avoided as much as possible. In this case, writing:
if (++$x === 3)
{
$x = 0;
}
Does exactly the same thing, and isn't that much more code, though it does look a lot nicer. Even so, the increment in an if statement is still messy, best increment beforehand, or:
$x = $x === 2 ? -1 : $x;
$x++;
If you've got a strange preference for ternaries... and if it has to be a one-liner:
$x = ($x ===2 ? -1 : $x) +1;
works, too... but there's a code-golfing site for these kind of things...
Why don't use module operator for that ?
$x = ++$x % 2;
I have something simple I'm trying to accomplish with less repetition.
By default, I want a div to be shown, however if $x == 1, then check to see if $y != 1, and if $y doesn't, then don't show the block.
However the best I can come up with is the following:
if($x) {
if($y != 1) {
echo '<div>display block</div>';
}
} else {
echo '<div>display block</div>';
}
This seems a bit repetitive.
I know I can tweak it a bit and do something like:
$displayBlock = '<div>display block</div>';
if($x) {
if($y != 1) {
echo $displayBlock;
}
} else {
echo $displayBlock;
}
But even still, I have a feeling that there is a way to do this whole if if else thing which I can't see right now.
How do you accomplish the above with less if statements? So: if $x != 1 (default), then show the displayBlock. if $x == 1, and $y != 1, then show the display block. If $x == 1 && $y == 1, then do not show the displayBlock.
if (!$x || $y != 1) echo $displayBlock;
+1 to zerkms's answer - it's on the money. To help you solve problems like this in the future, it might be handy to look at truth tables Karnaugh maps.
You essentially have two checks:
a) $x (coerced to true or false)
b) $y != 1
$y != 1
T F
$x T 1 0
F 1 1
So, from that you can see that if $x is falsey, or $y != 1 is true, then you should show the display block, hence:
if (!$x || $y != 1) echo $displayBlock;
In order to keep the amount of code down, you could use a more mathematical approach rather than logic; e.g.
<?php
if($x+$y != 2){echo $displayBlock;}
?>
The display box only stays off when the sum of x and y equals 2.
Check these out. http://www.php.net/manual/en/language.operators.logical.php
$displayBlock = '<div>display block</div>';
if((($x) && ($y != 1)) || (!$x)) {
echo $displayBlock;
}
How did we come to this solution?
Look at your statement, and dissect it logically:
If x is true ... and ... y is 1... then print.
Which brings us to:
(($x) && ($y != 1))
See? X is truth AND y is one. That brigns you down to
if (($x) && ($y != 1)) {
//Do that thing
} else {
if (! $x) {
//Do that thing
}
}
Which we can write simply as...
if (($x) && ($y != 1)) {
//Do that thing
} else { if (! $x) {
//Do that thing
}
Okay, so what's this say?
If conditionA do something, or if condition B do something.
Oh, there's an OR.
So, condtion A || condition B
Which of course, brings us back to...
if((($x) && ($y != 1)) || (!$x)) {
I put in more braces than required in there so you can see the flow of things.