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
Related
This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 2 years ago.
$value = $simpleXmlDoc->SomeNode->InnerNode;
actually assigns a simplexml object to $value instead of the actual value of InnerNode.
If I do:
$value = $simpleXmlDoc->SomeNode->InnerNode . "\n";
I get the value. Anyway of getting the actual value without the clumsy looking . "\n"?
Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so
$value = (string) $xml->someNode->innerNode;
You don't have to specify innerNode.
$value = (string) $simpleXmlDoc->SomeNode;
What about using a typecast, like something like that :
$value = (string)$simpleXmlDoc->SomeNode->InnerNode;
See : type-juggling
Or you can probably use strval(), intval() and all that -- just probably slower, because of the function call.
Either cast it to a string, or use it in a string context:
$value = (string) $simpleXmlDoc->SomeNode->InnerNode;
// OR
echo $simpleXmlDoc->SomeNode->InnerNode;
See the SimpleXML reference functions guide
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
This question already has answers here:
How to check if a string starts with a specified string? [duplicate]
(5 answers)
Closed 8 years ago.
I was looking and found How to check if variables starts with specific string & How to chech if a variable contains specific string but, I don't get it.
Which one could I use to check if a variable STARTS with a specific string just like a MYSQL LIKE WHERE CLAUSE:
SELECT * FROM table WHERE column LIKE 'string%'
You could use this, to match for any length string. Instead of hardcoding the string and it's length.
$string = "string to test";
$testfor = "strin";
if(substr( $string, 0, strlen($testfor) ) === $teststr) {
echo "Match";
}
if(substr($string, 0, 5) === "String") {
# Do code
} else {
# Error
}
Easy enough for simple strings.
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
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.