I have a PHP function that in echoing out a string, is losing some of the characters passed in it. Any reason why this would be happening?
I pass: $vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
It returns: #jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
This is my PHP code:
<?php
function display($str) {
echo $str;
}
display("$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX");
?>
If you have $ in you string use single quotes:
display('$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX');
Otherwise PHP would try to use $vhkvdov as a variable name and replace it by its contents, what will be nothing as I expect that the variable is not set
PHP reference on strings
Note: During development I would set error_reporting in php.ini to
error_reporting=E_ALL | E_NOTICE;
and
display_errors=On
or
log_errors=On
error_log=PATH/TO/YOUR/LOG.file ; make sure this is writable by the web server
To see an error message like :
PHP Notice: Undefined variable: vhkvdov in
The problem is that you are using double quotes, which make PHP interpret the inner string for PHP variables.
Consider the following example:
$friend = "foo";
echo "hello $friend";
// Output: hello foo
Use single quotes instead if you do not want PHP to interpret the string contents.
$friend = "foo";
echo 'hello $friend';
// Output: hello $friend
Take a read of this to help clarify why this is happening:
http://php.net/manual/en/language.types.string.php
Specifically:
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
This is whats happening with your code:
If a dollar sign ($) is encountered, the parser will greedily take as
many tokens as possible to form a valid variable name. Enclose the
variable name in curly braces to explicitly specify the end of the
name.
Here is an example:
<?php
$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
?>
The above example will output:
He drank some apple juice.
He drank some juice made of .
When you use single quotes, you will avoid this issue as the string taken litterally:
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
Related
I would like to replace array string values which contains multiple special characters to normal one.
Tried Code (array values):
$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = "Project Bribara<"${ENV_TEST}"#gmail.com>"
echo str_replace("${ENV_TEST}", $data['ENV_DEV'], $data['ENV_DEV']);
also tried
echo str_replace("\"${ENV_TEST}\"", $data['ENV_DEV'], $data['ENV_DEV']);
Expected:
"Project Bribara<rambo#gmail.com>"
Actual:
"Project Bribara<"${ENV_TEST}"#gmail.com>"
How can I get the expected output?
You should on PHP strings sometime. The important part about double quoted strings for your question is that you need to put a backslash before every $ and every " inside your string. Your code will then look like this:
$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = "Project Bribara<\"\${ENV_TEST}\"#gmail.com>";
echo str_replace("\${ENV_TEST}", $data['ENV_TEST'], $data['ENV_DEV']);
//also tried
echo "\n\n";
echo str_replace("\"\${ENV_TEST}\"", $data['ENV_TEST'], $data['ENV_DEV']);
If you use single quoted strings you don't need to escape $ (see the manual), and instead of \", you would need to escape single quotes (but there aren't any in your example).
$data['ENV_TEST'] = "rambo";
$data['ENV_DEV'] = 'Project Bribara<"${ENV_TEST}"#gmail.com>';
echo str_replace("\${ENV_TEST}", $data['ENV_TEST'], $data['ENV_DEV']);
//also tried
echo "\n\n";
echo str_replace('"${ENV_TEST}"', $data['ENV_TEST'], $data['ENV_DEV']);
I also fixed a missing semicolon and replaced DEV with TEST in one place.
String concatenation in PHP is done through the . operator. Your code would be:
$data['ENV_DEV'] = "Project Bribara<".$data['ENV_TEST']."#gmail.com>"
I am using this:
$variable = "Name is \"Bob\"";
$message = <<<EOF
<input type="text" value="$variable">
EOF;
And the result is :
Actually, this is synthetic example and I am working with db. But I tried: this synthetic example works (to simulate problem, actually it shows that what I am doing is not working).
Yes, the quotes will appear in the HTML.
Since the quotes will end the attribute value, you'll make the HTML invalid.
You need to make the variable HTML-safe with htmlspecialchars().
You are generating invalid HTML:
<input type="text" value="Name is "Bob"">
Please use htmlspecialchars() to encode $variable before insertion.
A heredoc is just a convenient shortcut for a multi-line echo. It doesn't care WHAT'S in the string, it'll just be output.
There is NO difference between the following two constructs:
$foo = "A string with an \" embedded quote";
echo <<<EOL
Hello, $foo, how are you
EOL;
echo "Hello, $foo, how are you";
The only real difference is that you don't have escape quotes in the rest of the string:
echo <<<EOL
This is a "quoted phrase" within a sentence
EOL;
echo "This is a \"quoted phrase\" within a sentence";
I cant get this to work and its driving me mad, pls help
echo "<a href='#' onclick='javascript:$.jGrowl
(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
the problem comes down to the 'product_description' - those single ' marks are breaking it, what should i do sigh
EDIT: if i replace the .$_SESSION['product_description'][$i]. with a bunch of charecters it works, its not a problem with anything but PHP and those ''
Chances are the real problem lies with the $.jGrowl. Within double quotes, PHP tries to parse found variables. e.g.
$foo = 'foo';
echo "This is foo: $foo"; // output: This is foo: foo
So, to avoid this you need to escape the $ using \$ within the string...
echo "...\$.jGrowl..."
See this demo.
Keep this here for reference:
Escape them with a backslash, like you would with double quotes. e.g.
// which ever quote is used to encapsulate the string
// must be escaped within the output.
echo 'Hello, \'world!\''; // output: Hello, 'world!'
echo "Hello, \"world!\""; // output: Hello, "world!"
// but, if you use the opposite quote, it does not need
// to be escaped for output.
echo 'Hello, "world!"'; // output: Hello, "world!"
echo "Hello, 'world!'"; // output: Hello, 'world!'
See the PHP Docs on strings for more information and what characters need escaping.
Are you missing your opening "?
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Make it
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Try:
echo "<a href='#' onclick='javascript:\$.jGrowl(\"".str_replace('"','\"',$_SESSION['product_description'][$i])."\");' >?</a>";
You need to ensure you are escaping any double quotes within the value...
This one works for me:
echo "<a href=\"#\" onclick=\"javascript:$.jGrowl('".$_SESSION['product_description'][$i]."')\" >?</a>";
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
I am new to PHP and in programming i have seen use of both "" and ' '.
What is the difference between "" and ' '?
And in declaring a link i have used the following ,but it does not seem to work there must be something wrong in quotation:
$abc_output .='' Back to Main Menu'';
echo $abc_output;
What might be the error here?
You want to keep the text inside of your string:
$abc_output .='Back to Main Menu';
The difference between ' and " is that you can embed variables inside of a double quoted string.
For example:
$name = 'John';
$sentence = "$name just left"; // John just left
If you were to use single quotes, then you'd have to concatenate:
$name = 'John';
$sentence = $name.' just left'; // John just left
PS: Don't forget that you always have to escape your quotes. The following 2 are the same:
$double = "I'm going out"; // I'm going out
$single = 'I\'m going out'; // I'm going out
Same applies the other way around:
$single = 'I said "Get out!!"'; // I said "Get out!!"
$double = "I said \"Get out!!\""; // I said "Get out!!"
Double quotes allow additional expressions, such as "$variable \n", which single quotes don't. Compare:
$variable = 42;
echo "double: $variable,\n 43\n";
echo 'single: $variable,\n 43';
This outputs:
double: 42,
43
single: $variable,\n 43
For more information, refer to the official documentation.
Text in double quotes are parsed.
For example:
$test = 'something';
print('This is $test');
print("This is $something");
would result in:
This is $test
This is something
If you don't need the string to be parsed you should use single quotes since it's better performance wise.
In your case you need to do:
$abc_output .='Back to Main Menu';
echo $abc_output;
Or you will get an error.
The Back to Main Menu wasn't in the string.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
Hi all
I am very new to PHP and I wanted to know what is the difference in use of ' ' and " " ?
Thanks
$name='RkReddy';
echo "$name hi hello"; //op-RkReddy hi hello
echo '$name hi hello';//op-$name hi hello
Double quotes evaluate the content of the string, the single quotes don't.
$var = 123;
echo 'This is the value of my var: $var'; // output: This is the value of my var: $var
echo "This is the value of my var: $var"; // output: This is the value of my var: 123
It WAS a perfomance issue too, 'cause evaluation time affects the interpreter, but nowadays with the current (minimum) hardware it's not an issue anymore.
Nothing much, but when using double quotes you can use a PHP variable inside the string, and the string will output the variable's value. When using a single quote it will output the variable's name without parsing the actual value. IE.:
$something = "A nice little variable";
echo "$something is printed out"; //output: A nice little variable is printed out
echo '$something is not printed out'; //output: $something is not printed out
Hope this helps!