PHP Echo post parameter surrounded by strings [duplicate] - php

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?

Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.

Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>

There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);

Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...

Related

I need to concatenate string with new line [duplicate]

This question already has answers here:
PHP new line break in emails
(8 answers)
Closed 7 years ago.
I have string
$st1="string 1"; $str2="string 2"; $str="string 3";
I need to make a above strings like this
$data='string 1
string 2
string 3';
i tried string concatenation
$data=$str1 . $str2 . $str3;
But I get output like this
$data = 'string 1 string 2 string 3;
any other method is there to get output like above?
Try to use as below :
$data=$str1 ."<br>". $str2 ."<br>". $str3;
$data = "{$str1}\n{$str2}\n{$str3}";
\n (in "") is a symbol of newline
try this
$data= $str1."<br/>".$str2."<br/>".$str3;
Use <br /> tag line break
$dta=$str1 . '<br />' . $str2. '<br />' . $str3;

Print variable within html code [duplicate]

This question already has an answer here:
PHP $_Server Not Working Properly [duplicate]
(1 answer)
Closed 8 years ago.
$a="something";
echo ' <div id="content">$a, but not sure how to do it</div>'
How can I print $a's value inside echo?
echo " <div id=\"content\">$a, but not sure how to do it</div>";
Please read the difference between single and double quotes in PHP.
echo "<div id=\"content\">" . $a . ", but not sure how to do it </div>";
The \'s are escape sequences that allow you to have quotation marks within strings (otherwise they would terminate the string). The .'s are concatenation.
there are many ways
1) use " instead of '
echo " <div id='content'>$a, but not sure how to do it</div>"
2) use {} with " if you are want to print something complex and want your editor to format it correctly. most of the time i use it with arrays or when calling a function ex. {$arr['a']} instead of $arr['a'].
echo " <div id='content'>{$a}, but not sure how to do it</div>"
3) just do this
echo ' <div id="content">' . $a . ', but not sure how to do it</div>'

Syntax and Variables in PHP

I'm having some trouble with writing some syntax. I want to echo this
'location_1' => $location_1,
However, it is not as simple as it seems. When I write the echo statement the integer 1 must be the variable $z. Here is the code I attempted to write
echo "'location_' . $z . '' =>' . ${'location_' . $z} . ','";
This is what it outputted
'location_' . 1 . '' =>' . something . ','
$location_1 is equal to the string something. I'm lost at how to do this the right way. Any guides on describing how this syntax works would be a major help too so I can understand it completely.
You can just write variables directly into double quoted strings see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
echo "'location_$z' => \$location_$z,";
You might want to also read the rest of the strings doc
This is the link to the echo documentation (see the examples, I think they described well how it works)
You can break it into two lines and get the expected output.
For example:
$var_location = "$". "location". $z;
echo "'location_" . $z . "' =>'" . $var_location . "','";
One way is: echo "'location_{$z}' => \$location_{$z},";
Edit: Is this what you meant?
<?php
$z = 1;
$location_1 = 'something';
echo "'location_$z' => " . ${'location_'. $z} . ',';
which produces: 'location_1' => something,
Why don't you store these variables inside an array for easier access. Something like:
$locations = array('location_id' => 'location_name');
Here's one way:
echo "'location_$z' => \$location_$z,";
You need to escape the $ symbol. The double quotes represent the thing to echo in this case, whereas the single quotes actually get echoed.

PHP echo does not work

I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";

HTML & PHP : Doublequotes-Singlequotes Issue [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
in HTML
we can
face='Tahoma'
face="Tahoma"
and in PHP
$name = "Junaid";
$name = 'Junaid';
$names = array('Junaid','Junaid','Junaid');
$names = array("Junaid","Junaid","Junaid");
now all these statements are correct with single or double quotes but
what difference does it make
which is the preferred method
what types of quotes to use where
which one of the following is correct
$link = "www.anyLINK.com"
echo "<a href=' ". $link ." '>Click Here</a>"
echo "<a href= ". $link ." >Click Here</a>"
The difference between single and double quotes in PHP is that PHP will read variables inside of double quotes but not single. For example:
<?php
$variable = "test";
echo "Can you see this $variable"; // Can you see this test
echo 'Can you see this $variable'; // Can you see this $variable
?>
The single quote will be read literal, where was double will attempt to replace the $variable with it's value.
Optimization Differences
As pointed out in the comments below, single quotes tend to be faster than double. In a quick benchmark, double quotes with any $'s escaped is the fastest vs single and double with and without $variables in the string. See http://codexon.codepad.org/54L3miwN
See http://php.net/manual/en/language.types.string.php.
In particular, variables are expanded in double quotes:
$foo = 42;
print('foo is $foo'); // foo is $foo
print("foo is $foo"); // foo is 42
In HTML, it doesn't matter at all.
In PHP, it does. Using the single-quotes prevents variables from being interpreted; for instance, echo '$foo'; will print "$foo". Not the variable, just the characters. Also, you have to escape single-quotes within single-quotes, but not single-quotes within double-quotes, etc. I answered this question before here.
As for your second question, they're both wrong. It should be:
echo "<a href='". $link ."'>Click Here</a>"
or, better yet:
echo "<a href='$link'>Click Here</a>"
or, better still, a templating engine like Smarty TPL.

Categories