Why php print "b1" in such code [duplicate] - php

This question already has answers here:
Echo and Print statement
(4 answers)
Closed 9 years ago.
<?php print("a")."b".print("с"); ?>
Result: сab1
Why php print "b1" in this code

Equates to:
print("c").... output the value "c" and return a value of 1 to
indicate success
print "a", concatenated with "b", concatenated with
the result of print("c") (which is "1") giving cab1

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

how to use '{' with variables in string (PHP) [duplicate]

This question already has answers here:
What does it mean to escape a string?
(3 answers)
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
I want to output a string like this "{$myvar}" but it doesn't work.
if for example $var = 1;
echo "$var"; // output "1"
echo "{$var}"; // output "1"
echo "\{$var\}"; // output "\{1\}"
how do I get "{1}" as a result ???
thanks

PHP if condition number issue [duplicate]

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.

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

multi variables in python like $$var in PHP [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 9 years ago.
In PHP like below
<?php
$a = 'b'
$b = 'c'
echo $$a
?>
Output: c
Is there any similar kind of implementation in Python like $$..?
no .... not really you can do stuff like
a = 'b'
b = 'c'
globals().getattr(a,None)
or even better use a dict (you should really do this!!!)
env = {'a':5,'b':'a'}
env[env['b']]
but there is nothing like $$a

Categories