PHP: Text Processing preg_match function - php

<?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

Related

$variable after <br> is not showing in echo in php

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 echo error - same echoes, few work, few doesn't

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.

setting up and printing out a array variable

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.

Simple php .. not working...hmm Looks very simple

Please check the code below:
<?php
$d=2;
echo "the sum of the number is"."<sub>($d+1)</sub>";
?>
Its giving as output:
the sum of the number is <sub>(2+1)</sub>
Ideally I need the output to be "the sum of the number is <sub>3</sub>".
It works fine when we don't use the HTML tags <sub>...
How can I fix this?
try writing it as
<?php $d=2; echo "the sum of the number is"."<sub>".($d+1)."</sub>"; ?>
the quotes give it a string representation and thus not allowing you to perform addition.
Move the expression outside the quotes:
<?php
$d = 2;
echo "the sum of the number is <sub>" . ($d+1) . "</sub>";
?>

Simple arithmetic in PHP

Here is a simple php program which gives a strange output. Can anyone explain why it is coming like this and how to get the expected output?
<?php
$a=2;$b=3;
echo "<br> ADD:".$a+$b;
echo "<br> SUB:".$a-$b;
echo "<br> MUL:".$a*$b;
echo "<br> DIV:".$a/$b;
?>
Output:
3-3
MUL:6
DIV:0.66666666666667
Expected Output:
ADD:5
SUB:-1
MUL:6
DIV:0.66666666666667
It is because the string concatenation operator . has the same precedence as the add/sub operators, and all of them are left-associative. This means that evaluation proceeds left-to-right, so "<br> ADD:".$a is evaluated first and the result is added to 3. This particular string converts to zero and 0 + 3 = 3. Similar for the subtraction.
Solution: put the arithmetic in parentheses.
echo "<br> ADD:".($a+$b);
echo "<br> SUB:".($a-$b);
On the other hand, mul/div have higher precedence than concatenation so they produce the expected result.

Categories