Am using $.get to send some values when decoding them in php they are all converted to strings even though they are numbers.
CODE:
var id_from = 19;
var id_to = 19;
$.get("save.php",{id_from:id_from,id_to:id_to }, function(){
console.log(result)
});
On the save.php
<?php
var_dump($_GET)
?>
Am getting
array(2) {
["id_from"]=>
string(1) "1"
["id_to"]=>
string(1) "4"
}
How can i get the values as integers since they are output as strings
try this:
PHP
By typecasting you can do this.
$value = "4";
$val = (int) $value;
echo var_dump($value); //string(1) "4"
echo var_dump($val); // int(4)
You can use the intval() function to convert it to int
<?php
echo intval($_GET['id_from'])."|".$_GET['id_from'];
?>
You can type cast in PHP Server side.
$id_from = (int) $_GET['id_from']
Related
it seems to be a really easy question, but I am a little bit struggling: I am receiving a JSON String via JavaScript. Now I would like to iterate through the element. The resulting string has this form: {"title":value,"title2":value}
How can I iterate through this JSON string without knowing the key and value? I would like to get this output:
title -> value
title2 -> value2
I tried it this way:
$json = file_get_contents('php://input');
$array = json_decode($json,true);
$response = "Test";
foreach($array as $key=>$val) {
$response = $response. "$key : $val";
}
echo json_encode($response);
It only returns "Test". If I change it to echo json_encode($array), it returns the mentioned JSON String.
You mention javascript and php in your question, so I'm going to answer for both. Here is JS, two different ways. I believe that foreach is being deemphasized in favor of the (of) construct now, but I don't work primarily in JS:
var json = '{"title": 12, "title2": "text"}';
var data = JSON.parse(json);
Object.keys(data).forEach(function(key) {
console.log(key + ' -> ' + data[key])
})
for(key of Object.keys(data)) {
console.log(key + ' -> ' + data[key]);
}
And for PHP:
You can parse the json string into an array using json_decode:
$json = '{"title": 12, "title2": "text"}';
$arr = json_decode($json, true);
foreach($arr as $key=>$val) {
echo "$key : $val";
}
true parses it into an array instead of a std object.
https://www.php.net/manual/en/function.json-decode.php
Because of response's format you must decode the decoded format in order to take the object as you want
$json = '{"title": 12, "title2": "text"}';
$encoded=json_encode($json);
$decoded=json_decode($encoded);
$ddecode=json_decode($decoded);
foreach($ddecode as $key=>$val) {
echo "$key -> $val";
}
Output :
title -> 12 title2 -> text
After a call to a php method that loads a value from a table, I am expected to be returned the number 10 but instead I get returned the following weird string:
string(1) "9" 10
How can I extract the value 10 alone? What kind of index does "9" represent?
I have tried also with echo json_encode($newNo); in php but it shows an error.
html:
document.getElementById("output").value = this.responseText; //string(1) "9" 10
php:
$number = 9;
$newNo = $number + 1;
echo $newNo;
I'll be obtaining jsvariable from on onclick javascript from the user. The user will input how much money they are spending in $xx.xx amount. It will come in, in string form. I need to do math on the amount, so I need it as a float.
I set it to a string as an example. I extract it from javascript into a php string variable:
<script> var jsvariable = "2.25"; </script>
<?php $x = "<script>document.writeln(jsvariable);</script>";
echo $x; var_dump($x); ?>
The echo $x shows 2.25. The var_dump shows that $x is a string(38) "2.25 "
So it's showing the $x variable now in php to be a 38 char string but it contains only 5 characters.
Then I try converting it with settype function. Also try re-casting it. It's not converting it.
<?php
settype($x, "float");
echo "After settype x is "; echo $x; var_dump($x);
$y = (float)$x ;
echo " y cast y is "; echo $y; var_dump ($y);
?>
After settype x is 0 float(0)
After casting y is 0 float(0)
How do I convert the $x string into a float number that's the right value of 2.25 ?
Just cast it like this: $y = (float)$x. You might want to check out type juggeling as well as String conversion to numbers.
I am trying to convert a PHP function to javascript, but i cant read the following line of codes:
$sha_string .= "$key=$value$ipn_passphrase";
and
$sha_sign = strtoupper(hash("sha512", $sha_string));
Complete function:
function digistore_signature( $ipn_passphrase, $array)
{
unset($array[ 'sha_sign' ]);
$keys = array_keys($array);
sort($keys);
$sha_string = "";
foreach ($keys as $key)
{
$value = html_entity_decode( $array[ $key ] );
$is_empty = !isset($value) || $value === "" || $value === false;
if ($is_empty)
{
continue;
}
$sha_string .= "$key=$value$ipn_passphrase";
}
$sha_sign = strtoupper(hash("sha512", $sha_string));
return $sha_sign;
}
the $array is the body of a POST request.
the $passphrase is a string
.= in PHP is a simple concatenation. it is similar to the programming concept of +=. It's easy to understand with an example
<?php
$a = "hello";
$a .= " "; //now $a = "hello "
$a .= "world"; // now $a = "hello world"
"$key=$value$ipn_passphrase"; is called an in-place variable substitution in PHP. You can check the PHP Doc for more. You can simply consider it as the value $key, $values and $ipn_passphrase is replaced by the values of those variables accordingly and it forms a new string variable $sha_sign.
$sha_sign = strtoupper(hash("sha512", $sha_string)); is a simple statement where you pass the algorithm sha512 and $sha_string to the function hash() and store the result back in $sha_string variable.
I am trying to get the browser timezone via JavaScript and capture it in a php variable with ob_start() and ob_get_clean. The value set by ob_get_clean shows as a string, but when I try to cast it to an int in php, the value goes to 0.
It doesn't matter how I cast this string, (int) $offset or intval($offset) or $offset * 1 the string starts as 420, but the result of casting it to an integer changes the value to integer 0.
<?php
ob_start();
?>
<script>
var d = new Date();
document.write(d.getTimezoneOffset());
</script>
<?php
$offset = trim(ob_get_clean()); // timezone offset Colorado = 420 minutes
echo $offset; // 420
echo gettype($offset); // string
$offset += 0;
echo gettype($offset); // integer
echo $offset; // shows 0 - I expect the value to be 420
I expect $offset to be an integer with a value of 420. Now it I try similar code with a regular string, I get the expected results.
$strnum = '530';
echo gettype($strnum); // string
$strnum += 0;
echo gettype($strnum); // integer
echo $strnum; // 530
?>
I can't see why the two examples don't work the same.
You can't to this, you can't catch the JS script output in php, you need to pass it throught ajax call. Check the SOURCE OUTPUT of the page and not the screen output.
screen output (in my case):
-60 string integer 0
REAL OUTPUT (SOURCE):
<script>
var d = new Date();
document.write(d.getTimezoneOffset());
</script>
string
integer
0
why u not use cast (int) or intval() function
example :
$var = (int) $yourstring
OR
$var = intval($youtstring)
Try this out
$offset = 'timezone offset Colorado = 420 minutes';
preg_match_all('!\d+!', $offset, $matches);
echo $offset; // 420
print_r( $matches); //array
echo $matches[0][0]; //420
$matches[0][0] += 1;
echo $matches[0][0];