PHP if condition number issue [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
The php code is returing true in both cases show below. I dont know why?
<?php
$cid = 150;
if ($cid=100)
{
echo $cid;
echo "<BR>";
}
if ($cid==100)
{
echo "NEW";
echo "<BR>";
echo $cid;
echo "<BR>";
}
?>
The output is:
100
NEW
100
Why is the if condition not working?

In the first if-statement you are assigning 100 to $cid, not comparing. You're using a single = instead of ==. So in the first statement $cid is set to 100. When it comes to the second if-statement, $cid has a value of 100. So the conditional evaluates in a truthy value.

Related

json_encode add numbers to result? [duplicate]

This question already has answers here:
Can I rely on PHP php.ini precision workaround for floating point issue
(5 answers)
Closed 3 years ago.
I am having a very different question. For some reason, when the json_encode function receives a variable that has the value assigned by a multiplication, the echo result will be a different value than expected. Example:
<?php
$test = 1.1 * 122;
echo json_encode(array("test" => $test)); // prints {"test":134.20000000000002}
echo $test; // prints 134.2
?>
For some reason, it doesn't work on every version of PHP, so I created a snippet on a tester that works:
Online Tester
Why does this happen?
Just use round function
$test = round(1.1 * 122, 2);
echo json_encode(array("test" => $test));

PHP how to check if variable starts with 'pa_' or not? [duplicate]

This question already has answers here:
Check if variable starts with 'http'
(6 answers)
Closed 3 years ago.
I have a variable and i want to check if it starts with 'pa_' how can I do that?
i have tried this but it does not work
$test_str = 'pa_';
if(substr( $product_attribute['name'], 0, strlen($test_str) ) === $test_str) {
$pa_array[]= $product_attribute['name'];
}
Just check if pa_ is at the first position of the string
if (strpos($product_attribute['name'], 'pa_') === 0) {
$pa_array[]= $product_attribute['name'];
}
Try dd($pa_array) within the if-block to see if you even put it in the array.
I quickly tried this and it worked.
Use strpos or access chars in a string by using an offset value:
$paName = $product_attribute['name'];
if($paName[0] . $paName[1] . $paName[2] == $test_str) {
$pa_array[]= $paName;
}

String to int/float [duplicate]

This question already has answers here:
checking if a number is float in PHP
(5 answers)
Shortest way to check if a variable contains positive integer using PHP?
(6 answers)
Closed 5 years ago.
I have a number that comes as a string from an input. It could be "450.021" or "30". I need to determine whether it is int or a float number. How do I do that?
I tried with intval but apparently floating numbers are floored and in my case it is not going to help me.
Very naiv approach
foreach(["342.23", "30"] as $a) {
if( (float)(int)$a === (float)$a ) {
echo "int";
} else {
echo "float";
}
}
But this won't catch cases where you parse "340.0".
Edit: Digging a bit more:
foreach(["342.23", "30"] as $a) {
$is_float = filter_var($a, FILTER_VALIDATE_FLOAT);
$is_int = filter_var($a, FILTER_VALIDATE_INT);
if( $is_float && $is_int ) {
echo $a . " is Int";
} else if( $is_float && !$is_int ) {
echo $a . " is Float";
}
}

strpos() madness- why doesn't it work? [duplicate]

This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 7 years ago.
See the following issue:
$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";
if(strpos($str,$search)) {
echo "True";
}else {
echo "False";
}
Why in the world does $str = "video-23984" return false? And what can I do to make it return true?
In the first string the word video is in the first position, which means 0.
In the second it's in the second position, which means 1.
Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.

Evaluate multiple conditions stored as a string withput eval method [duplicate]

This question already has answers here:
PHP - if condition inside string
(5 answers)
Closed 8 years ago.
I have a string like this:
$str = "0 || 0 && 1";
actually this string is a condition.
if i do like this :
if($str) {
echo "done";
}
else {
echo "sfcsd";
}
it is always true since $str is string.
How can i evaluate this string with out eval().
check out sandboxing in PHP. here is a quick tutorial:
http://www.fieryprophet.com/blog/detail/sandboxing-untrusted-code-with-phpsandbox

Categories