String to int/float [duplicate] - php

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";
}
}

Related

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;
}

Checking if a string contains " or ' [duplicate]

This question already has answers here:
Simple PHP strpos function not working, why?
(5 answers)
Closed 3 years ago.
I am trying to do a input check and to seeif the string contains " or '. I have the stringcheck method that looks like this
public static function stringcheck($search, $string)
{
$position = strpos($string, $search);
if ($position == true)
{
return true;
}
else{
return false;
}
}
And this is how i am trying to check it
if(H::stringcheck($search8, $string) === true || $string[0] === """)
{
$value++;
}
And the $search8 look like $search8 = htmlspecialchars('"');. I should also mention that the $string is already sanitized.
strpos returns the position of the match, which will be 0 if the match is at the beginning. But 0 is not == true.
I guess you are trying to work around that by extra checking $string[0] === """ which won't work, as I commented above: $string[0] is a single character, but " is 6 characters long.

Php do not make distinction in if statement with capital letters [duplicate]

This question already has answers here:
Is == in PHP a case-sensitive string comparison?
(7 answers)
Closed 4 years ago.
I have an if statement in my website. And what I want, is that he won't make distinction in capital letters. My if statement:
<?php
$nameOne = "JOHN";
$check = "john";
if($nameOne == $check){
echo 'No distinction';
} else {
echo 'distinction ';
}
?>
So, I want to echo 'No distinction'.
You can make the comparison using strtolower() on both strings
if (strtolower($nameOne) == strtolower($check))
You can use the solution suggested by #NicoHaase
<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
https://secure.php.net/manual/en/function.strcasecmp.php

PHP Conditional to see if string contains a variable [duplicate]

This question already has answers here:
Problem with Strpos In PHP
(3 answers)
PHP strpos() function returning incorrect result
(1 answer)
Closed 5 years ago.
I'm struggling to figure out how to use PHP strpos to find a variable in a string. The code below works if I enter a number directly but not if I replace the number with a variable I know to be a single number. No PHP errors present but the conditional renders false. I've been searching all over and am stumped by this.
The $string variable returns "253,254,255".
The $current_page_id variable returns "253".
$string = "";
foreach( $page_capabilities as $post):
$string .= $post->ID.',';
endforeach;
if (strpos($string, $current_page_id) !== false) {
// This works
//if (strpos($string, '253') !== false) {
// This does not
if (strpos($string, $current_page_id) !== false) {
echo 'true';
}
}
You don't need that CSV string to check for the ID you're looking for. You can just do it in the foreach loop where you're currently building the string.
foreach ($page_capabilities as $post) {
if ($post->ID == $current_page_id) {
echo 'true';
break; // stop looking if you find it
}
}
You should cast $current_page_id to a string:
if (strpos($string, (string)$current_page_id) !== false) { ...
The following is from php docs:
If needle is not a string, it is converted to an integer and applied
as the ordinal value of a character.
Which means you were comparing "253,254,255" with the character corresponding to 253 in the ASCII table
You need to cast the integer to a string.
$postIDs = [55, 89, 144, 233];
$string = implode(',', $postIDs);
$postID = 55;
if(strpos($string, strval($postID)) !== false) {
echo 'strpos did not return false';
}
I would suggest you to change your approach. You should search for the exact string value. Because as #mickmackusa suggested, it would find 25 & 53 too
$page_capabilities = array(array('POSTID' => 253),array('POSTID' => 254),array('POSTID' => 255));
$current_page_id = '255';
$key = array_search($current_page_id, array_column($page_capabilities, 'POSTID'));
if($key !== false) {
echo 'true';
}

PHP false-positives [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
So I have this function and I'm trying to understand how this is true and how it comes out false if I use === instead of == .
function is_equal($value1, $value2) {
$output = "{$value1} == {$value2}: ";
if ($value1 == $value2) {
$output = $output . "true<br />";
} else {
$output = $output . "false<br />";
}
return $output;
}
echo is_equal("123", " 123");
echo is_equal("123", "+0123");
?>
this code above comes out true because I'm testing for == how is that? and also if I use === it's false
When you compare equality using ==, PHP will juggle the types. I suspect your types are being juggled resulting in a numeric comparison.
When you compare equality using === the type is compared first, followed by the values.
Yes, this is right. === will compare the value and the typeinstead of == that is comparing if values are identical.
You can also try this one :
echo is_equal("123", 123);
=== tests whether the two variables are identical (same value, same type). == tests for equality and does type juggling for you.
Read up over here.

Categories