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

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

Related

Add two input values as string in Codeigniter [duplicate]

This question already has answers here:
String concatenation in PHP
(2 answers)
Closed 4 years ago.
I´m trying to add two input values as string not as number, the problem is that they are actually numbers.
'clabe' => $this->input->post('clabe2') + $this->input->post('dc')
Where clabe2 = 123 and dc = 4;
I want to get result as 1234 not 127
Any help will be appreciated
Concatenate this and assign to variable
like this:
$var = $this->input->post('clabe2') . $this->input->post('dc');
'clabe' => $var
Do something like this
Use concatenation operator . for string concatenation
'clabe' => $this->input->post('clabe2') . $this->input->post('dc');
For more : http://php.net/manual/en/language.operators.string.php

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

Can you refer to a var using a string in PHP? [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 8 years ago.
What I mean is can I do something like this...
$number = 1;
$varname = 'number';
Now I want to get the value of $number by using $varname so something like...
echo $($varname);
Output:
1
You need to use {} instead of ():
echo ${$varname};
Or even shorter:
echo $$varname;
Which equals:
echo ${'number'};
But as kingkero pointed out: You probably want to do something like ${'number'.$index} and that is easier solved with arrays.

Undefined index: string in (...) [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I need to switch on E_ALL and get the in the title mentioned warning, when I am exploding the $_GET string:
$input = explode( '/', $_GET['string'] );
Where does that come from? Is it the missing 3rd parameter (limit) for explode ? I want all entries.
Cheers
You need to make use of the isset construct first.
<?php
if (isset($_GET['string'])) {
$input = explode('/', $_GET['string']);
} else {
echo "The string was not passed!";
}

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

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

Categories