Is it possible to put quotes inside quotes?
If so how?
Here is my code:
<?php
echo '<span onclick="$(this).addClass('selected');"> </span>';
?>
According to php.net
To specify a literal single quote, escape it with a backslash (\).
It means you could have:
<?php
echo '<span onclick="$(this).addClass(\'selected\');"> </span>';
?>
Heredoc is the perfect solution for this:
<?php
echo <<< HereDocString
<span onclick="$(this).addClass('selected');"> </span>
HereDocString;
?>
You could do this:
<?php
// some code
?>
<span onclick="$(this).addClass('selected');"> </span>
<?php
// some more code.
?>
Related
I have delared variables like this. I have created more than 100 variables in every page.
<?php echo $rsedit[customer_name]; ?>
<?php echo $rsedit[address]; ?>
<?php echo $rsedit[city]; ?>
<?php echo $rsedit[pincode]; ?>
<?php echo $rsedit[contact_no]; ?>
<?php echo $rsedit[email_id]; ?>
I am searching and replacing in notepad++. But it takes long time.
I want following output:
<?php echo $rsedit['customer_name']; ?>
<?php echo $rsedit['address']; ?>
<?php echo $rsedit['city']; ?>
<?php echo $rsedit['pincode']; ?>
<?php echo $rsedit['contact_no']; ?>
<?php echo $rsedit['email_id']; ?>
Tell me the best solution for this to replace together.
I would use the following find and replace, in regex mode:
Find: <\?php echo \$(.*?)\[(.*?)\];\s*\?>
Replace: <?php echo \$$1['$2']; ?>
Demo
Provided all those 100 lines always begins with "echo $rsedit"
in regular notepad you could just CTRL+H(windows) or edit > replace
Then add "rsedit[" on find what
Then add "rsedit['" on replace with //notice the single quote after rsedit[
then tap replace all
to add single quote at the end again CTRL+H(windows)
Then add "];" on find what
Then add "'];" on replace with //notice the position of the single quote
then tap replace all
If the variables are different say
<?php echo $rsedit1[customer_name]; ?>
<?php echo $somethingelse[address]; ?>
<?php echo $rsedit3[city]; ?>
<?php echo $rsedi6[pincode]; ?>
<?php echo $ftedit[contact_no]; ?>
<?php echo $rsedit[email_id]; ?>
Just look for something that is common and use it as a replacement point.For the above code you could replace "[" with "['" notice the single quote
then replace ] with ']
I am not able to find the error why my title is not displaying. This is my code:
echo '<br /><b style="display:block";>'.$name .'</b>';
echo '<i style="color:red";>'.$class.'';
Can you please help me?
Remove the unnecessary last single quotes '', Also put your semicolon ; inside the double quotes "
<?php
$title = 'this is title value'; //example title
$des = 'this is description'; // example description
echo '<br /><b style="font-size:1.5em;">'.$title.'</b>';
echo '<i style="font-size:18px;">'.$des;
?>
DEMO: https://eval.in/1044486
It is not recommended to echo HTML tags inside PHP script. Your code should have been written as follows. I see that closing tag </i> is missing as well. I hope this might solve your problem.
<br />
<b style="display:block;">
<?php echo $name;?>
</b>
<i style="color:red;">
<?php echo $class;?>
</i>
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome echo $_SESSION["username"];</li>';
In the above code it is not displaying username variable.. instead it is simply echoing Welcome $_SESSION["username"];
FYI.. I have used session.start() in both the files
But how to display Welcome 'username'
You have an error. you should close quotes.
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome' .$_SESSION["username"].'</li>';
If still doesn't show the username:
Turn on error_reporting or use var_dump to cehck if variable is set
<?php
error_reporting(-1);
<?php
var_dump($_SESSION);
and just note about templating. If you use this in a view (most of code is html) then try to use php temlating
<li>
<a href="signup.html">
<span class="glyphicon glyphicon-user"></span>Welcome <?= $_SESSION["username"] ?>
</a>
</li>
The part Welcome echo $_SESSION["username"]; is simply a STRING and would be echoed verbatim without any Evaluation, Processing or Parsing. And, by the way: You don't use an echo Statement within an echo statement. That should be like SAYING GOOD MORNING IN ENGLISH & GERMAN AT THE SAME TIME THROUGH THE SAME ONE SINGLE MOUTH: LOL :-)
You should have used Double Quotes to surround the String you wished to echo out and then use single quotes for the HTML DOM Element Attributes like "class" or "HREF". That way you, you could very easily echo your String without much fuss. Here's how:
<?php
echo "<li>
<a href='signup.html'>
<span class='glyphicon glyphicon-user'></span>Welcome {$_SESSION['username']}
</a>
</li>";
Try this
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome '. $_SESSION["username"] .'</li>';
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>
What would be the correct syntax for the onClick attribute in the following:
<?php
if($_resp['user']) {
echo '<div class="logo">Link Text';
}
?>
Since you are nesting both double and single quotes, you will need to escape the single quotes in the onClick with backslashes.
echo '<div class="logo">Link Text';
Consider using a HEREDOC
if ($_resp['user']) {
echo <<<EOL
<div class="logo">Link Text
EOL;
}
or dropping out of PHP mode:
if ($_resp['user']) { ?>
<div class="logo">Link Text
<?php }
Both get the job done, and eliminate any need to escape quotes inside the html/javascript you're generating
You can mix HTML and PHP, so you might want to simply move it out of PHP:
<?php
if($_resp['user']) {
?>
<div class="logo">Link Text
<?
}
?>