Hi I have some simple code here that as far as I can tell should be working but it doesn't and I am puzzled as to why
//VARIABLES
$rowcount=15;
$colcount=15;
$path= array( array());
$startrow=8;
$startcol=7;
$row=$startrow;
$col=$startcol;
for ($a=0;$a<$rowcount;$a++) {
for ($b=0;$b<$colcount;$b++) {
$path[$a][$b]=0;
}
}
echo"In case one ";
echo"<BR>";
echo " Col= ".$col;
echo " Row= ".$row-1;
echo " Col= ".$col;
echo " Cell= ".$path[$row-1][$col];
echo " Cellx= ".$path[7][7];
if($path[$row-1][$col]=="o") {
echo " F1 = ".$flagOne;
$flagOne="1";
echo " cell equals ==o==";
echo " F1 = ".$flagOne;
} else {
echo " cell not equal ==o==";
}
echo"<BR>";
This is what I get as a output
In case one
Col= 7-1 Col= 7 Cell= 0 Cellx= 0 F1 = 0 cell equals ==o== F1 = 1
what puzzles me is the 7th spot "-1"
I would expect to see "Row= 7" not "-1" since $row=8
I have been programming in java and C and lately just barely touching PHP lately but I have been doing PHP for years, I think this should be working
what am i doing wrong
As another issue
looking at the if statement the value of the array/cell = 0 so I would also expect the evaluation to be false and echo this
cell not equal ==o==
but the condition is evaluating true why is this
my one thought is the way I am setting up the $path variable
I simply want it to be a simple two dimensional array $path[$a][$b] is it setup correctly when I output the array I see what I am expecting an two dimensional array.
Never had to do these much in the past
$rowcount=15;
$colcount=15;
$path= array( array());
$startrow=8;
$startcol=7;
$row=$startrow;
$col=$startcol;
for ($a=0;$a<$rowcount;$a++) {
for ($b=0;$b<$colcount;$b++) {
$path[$a][$b]=0;
}
}
echo"In case one ";
echo"<BR>";
echo " Col= ".$col;
echo " Row= ".($row-1);
echo " Col= ".$col;
echo " Cell= ".$path[$row-1][$col];
echo " Cellx= ".$path[7][7];
if($path[$row-1][$col]==0) {
echo " F1 = ".$flagOne;
$flagOne="1";
echo " cell equals ==0==";
echo " F1 = ".$flagOne;
} else {
echo " cell not equal ==0==";
}
echo"<BR>";
At first, dont use double-quotes all over the script. There is a special meaning to double and single quotes in PHP, read the manual about this. Second - character "o" is not zero. You can clearly see, that You are trying to match zero against lower-case "o". Also, integers does not need to be quoted in PHP just as in about every language out there. Check Your font settings to differ one from another. Third - If You are doing both evaluation and concatenation in PHP, You should surround evaluation statement with parentheses, although it is a bad practice. It is better to pre-calculate values and assign them to values before sending them to output or before concatenating them with another values as strings.
Related
I'm learning basics in php, and I was trying to add multiple arguments in echo command. But the variable after <br> is not showing.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".$number1+$number2;
and the output should be:
number 1 is: 10
number 2 is: 20
30
But the output is:
number 1 is: 10
number 2 is: 2020
So what's the error?
used this code
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
$total= $number1+$number2;
echo "<br> ".$total;
?>
The output will be:
number 1 is: 10
number 2 is: 20
30
The other answers just state a solution, this answer explains whats happening and how to prevent the unexpected behavior in 2 ways.
The dot operator has the same precedence as + and -.
Considering
$number1 = 10;
$number2 = 20;
echo "<br> ".$number1+$number2;
The dot you've used is a string operator, not a numeric operator.
What's happening:
"<br>" and 10 are concatenated with the dot operator to "<br>10".
"<br>10" is added to $number2 (20) with the numeric + operator.
Non-empty, non-numeric strings are converted to 0. Meaning "<br>10" = 0.
0+20 results in 20 which makes line 3: echo 20;
This can be solved by changing the precedences by using brackets echo "<br> ". ($number1 + $number2); or the less seen option, by passing more arguments to the echo language construct: echo "<br> ", $number1 + $number2; (Note the comma instead of a dot). Each argument will be evaluated first before outputting them all together.
Personally I use the second option (multiple arguments) in cases like this.
Just add the braces to the sum operation.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".($number1+$number2);
The output will be:
number 1 is: 10
number 2 is: 20
30
You should revise code within bracket
echo "<br> ". ($number1 + $number2);
to get the result you want.
Reason: each operation has precendence level
Get reference: http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html
<?php
$eqn1="0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
preg_match("/\b[0-9]*\b/",$eqn1,$vx1);
echo "X1 is: $vx1[0]";
?>
Can someone tell me, how to store the value of x1 (that is, 0.068683000000003) in the variable $vx1?
The output is:
X1 is: 0
1)put semicolon after each sentences;
2)use echo "X1 is:" $vx1[0]; instead ofecho "X1 is: $vx1[0]";
<?php
$eqn1="0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
preg_match("/\b[0-9]*\b/",$eqn1,$vx1);
echo "X1 is:" .$vx1[0];
You are missing semicolons after statements and the echo statement has to be modified:
<?php
$eqn1 = "0.068683000000003x1+2.046124y1+-0.4153z1=0.486977512";
preg_match("/\b[0-9]*\b/", $eqn1, $vx1);
echo "X1 is: " . $vx1[0];
Your regex takes into account only integer, your first numùber is a decimal one.
Here is a way to do the job, the number you're looking for is in group 1:
$eqn1 = "0.068683000000003x1 + 2.046124y1 + -0.4153z1 = 0.486977512";
preg_match("/^(\d+\.\d+)/", $eqn1, $vx1);
echo "X1 is: ", $vx1[1], "\n";
Output:
0.068683000000003
This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
Please explain how echo understand the dot(.) with mathematical expressions and binary comma(,).
<?php
echo "The Sum: " . 2+3;
?>
//Output
3
Why 3 as output?
. and + are left-associative, so your statement is interpreted as
echo ("The Sum: " . 2) + 3;
This is equivalent to
echo "The Sum: 2" + 3;
When you add a string and a number, it converts the string to a number, which tries to find a number at the beginning of the string. Since "The Sum: 2" doesn't begin with a number, it converts to 0. So that makes the statement equivalent to
echo 0 + 3;
which simplifies to
echo 3;
and that's the result you see.
there is two operator dot(.) and plus(+) and dot has high priority so . try this
<?php
echo ("The Sum: " . 2) + 3;
?>
I have a really weird problem: Below in the code listed 4 echo's are pretty much the same, but only the last ones work properly (First two echoes only print the answer of addition/ subtraction, no text).
Here's the code:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
<?php $sk1 = $_POST["sk1"];
$sk2 = $_POST["sk2"];
$veiksm = $_POST["veiksmas"];
switch($veiksm){
default:
echo "Jus nepasirinkote veiksmo";
break;
case "sud":
echo "Sudeties veiksmo rezultatas: " .$sk1 + $sk2;
break;
case "ati":
echo "Atimties veiksmo rezultatas: " .$sk1 - $sk2;
break;
case "dal":
echo "Dalybos veiksmo rezultatas: " .$sk1 / $sk2;
break;
case "dau":
echo "Daugybos veiksmo rezultatas: " .$sk1 * $sk2;
break;
}
?>
</body>
</html>
You have to put brackets around your calculation like:
echo "Atimties veiksmo rezultatas: " . ($sk1 - $sk2);
//^ See here ^
Otherwise you can imagine your echo statement like this:
"Sudeties veiksmo rezultatas: 17" + 5 // Same as 0 + 5, because the string is casted to int which is 0
Also * and / works because they are getting evaluated first
Put parentheses around the calculation, like this:
echo "Sudeties veiksmo rezultatas: " . ($sk1 + $sk2);
The reason for this is the order in which the expression is processed. Without parentheses, PHP evaluates everything from left to right, so it will first concatinate $sk1 to the string. The combined value will be "Sudeties veiksmo rezultatas: 1" (if $sk1 is 1).
After that, $sk2 is added to that value. Because PHP cannot add up a string, it tries to convert it to a number. This conversion fails, because the string starts with a non-numeric text, and defaults to 0 which is added to the value of $sk2.
Multiplication and division operators have higher precedence, so they are evaluated first, overriding the left-to-right order. That's why it works for the last two cases.
I know the above code outputs 323
But need an explanation on how is it outputting 323
Anybody can pls explain?
Both echo and print are intended to behave more like language constructs and not functions. As such they have a flow of control. What happens here in your code is that you are calling print from inside of a language construct (echo). Meaning that print will send its output first before echo has completed its task (remember you called print from inside of echo).
To show you what's happening a bit more clearly, it actually has nothing to do with operator precedence at all.
echo ('a') . ('b' * (print 'c')); // ca0
// This is the same thing as...
echo 'a' . 'b' * print 'c'; // ca0
Notice the operators have no effect on the order of the characters in the resulting output here.
print always returns 1 so what happens here is that you performed an arithmetic operation on 'b' * 1, which is the stirng b multiplied by the return value of print. Thus why you see the output as c (print sent output before echo has even finished doing it's job), first, and then everything that echo was supposed to print.
Let me elaborate even further with the following example...
echo print 'a' . 'b' . 'c'; // abc1
Notice the 1 at the end of that output. This is because all echo did was output the return value of print and not the string. Instead, print is the one that provided the abc as output (and remember print is able to send output before echo can since echo has to wait to process everything inside of the construct before it can finish).
This makes it even more clear...
echo (print 'a') . 'b' . 'c'; // a1bc
Now the 1 comes right after a.
If you want echo to send the output for each expression individually, you can supply one argument for every expression you want processed and sent to output... So for example:
echo print 'a', 'b', 'c'; // a1bc
echo 2, 3 * print 3; // 233
I hope that clarifies it a bit more for you.
The reason is (print 3) has priority over the previous operators. If you write `echo (8) . (7 * (print 3)); you will get 387, for example.
Here
echo (2).(3*print(3));
Step1:(Starting execution)
echo (2).(3*print(3)); //Output = ''
Step 2:(Print action will take place)
echo (2).(3*1); // Will print 3 and returns 1 as per print function. Output = 3
Step 3:(Multiplication operation)
echo (2).(3); // Multiplication operation willtake place. Output = 3
Step 4:(Prints the data)
echo (2).(3); //The . Operator will used to concate the strings in php, thus Output = 323
Print has more priority then echo thus it prints 3 first later 2 and 3