why line break is not working in php code - php

I have added a line break in this piece of PHP code, but it's getting a syntax error.
Does PHP not allow breaks in its code?
<!DOCTYPE>
<html>
<head>
</head>
<body>
</body>
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
<br/>
echo "Gravity Value is".Gravity_Value;
?>
</html>

You can use \r\n if you want to break line within php.
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi. "\r\n";
echo "Gravity Value is".Gravity_Value;
?>

<br/> is not valid PHP syntax. You could either have the <br/> outside the PHP block:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
?>
<!-- PHP block terminated -->
<br/>
<!-- New PHP block opened: -->
<?PHP
echo "Gravity Value is".Gravity_Value;
?>
Or just echo it from PHP:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
echo "<br/>"; # Here!
echo "Gravity Value is".Gravity_Value;
?>

You need to write echo '<br/>';.
This is because you can either write HTML code or PHP within the specific sections.
You are currently in a PHP code section so you can only write PHP code there that will be valid.
<br/> is not a valid PHP code but HTML. However, because you are within <?php ?> it is not valid.

You can use the code below instead:
echo '<br/>';
<br/> is an HTML tag hence the syntax error you're getting.

<br> is html and must be echoed within php, like so:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
echo "<br/>";
echo "Gravity Value is".Gravity_Value;
?>
You can also use \r\n in php like so:
echo "Pi value is".Value_of_Pi. "\r\n";

Related

PHP - replacing dot into arrows

I have this data in my database that i fetched:
As you can see i store an html template in my database.
This is the code i used to output that html:
$php = Blade::compileString($template->content);
dd($php) //i used laravel framework, btw.
Then this is the output:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exmaple.com</title>
<style>
</style>
</head>
<body>
<div class="account-details">
<b>Account Name</b>: <?php echo e(company.name); ?><br>
<b>Plan</b>: <?php echo e(plan.name); ?><br>
<b>Source</b>: <?php echo e(source.name); ?><br>
<b>Source Type</b>: <?php echo e(source.source_type.name); ?><br>
<br>
</div>
<div class="welcome" style="font-family:Arial;font-size:small">
Hi <?php echo e(user.first_name); ?>,<br>
<br>
There seems to be a problem blah blah blah<br>
<br>
Details of the problem are:<br>
<?php echo e(sourceMessage); ?><br>
<br>
You can check and update the source here:<br>
https://example.com<br>
<br>
<br>
<span style="font-size:12.8px">
Kind regards,<br>
<br>
Test Team<br>
<br>
Email: example.com<br>
Website: example.com<br>
</span>
</div>
</body>
</html>
I want to change the .(dot) into -> so in this example, some of text will output like:
from <?php echo e(company.name); ?> to <?php echo e(company->name); ?>
from <?php echo e(source.name); ?> to <?php echo e(source->name); ?>
So i think if it's in a <?php echo e(whatever); ?> that's the time we check and replace the . with -> ?
I think this can be done by RegEx but I'm not expert on that.
The reason why i wanted to replace it is because i am getting a template from an email service then returns ., so i wanted to replace that with -> because I know PHP reads -> in accessing objects rather than ..
You can use preg_replace to look for pieces of code matching <?php ... e(f) and replace the .s in f with ->:
$html = preg_replace_callback('/(<\?php\s+.*?\be\()([^)]+\))/',
function ($m) {
return "{$m[1]}$" . str_replace('.', '->', $m[2]);
},
$html);
Note we use a callback as it makes it easier to deal with replacing a.b.c with a->b->c. Also, to really look like PHP, you need to add a $ at the beginning of the variable name, which this code does. If you don't want it, just change {$m[1]}$ to {$m[1]}
Demo on 3v4l.org
If you're doing that on a editor like VS code or sublime, you just need to replace the function e usage parts.
CTRL + H
(In sublime or VS code) to open up replace dialog.
Make sure the Regex option is clicked.
This would do the trick;
Search (e\(.+)\.(.+\))
Replace $1->$3

How can I mix PHP and HTML code?

I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>

How to echo a string which contains <?php ?>

I want to generate a php page which contains HTML and some php commands. Problem is that when I submit the code below, I get this output
<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>
rather than php commands. How do I fix this??
Thanks!!
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='0' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo '\$stfromyearErr ';?&gt&lt?php echo '\$stfrommonthErr
';?&gt&lt?php echo '\$stfromdayErr ';?&gt &lt?php echo '\$sttoyearErr ';?
&gt&lt?php echo '\$sttomonthErr ';?&gt&lt?php echo '\$sttodayErr ';?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
You're echoing out your PHP tags as <?php, and then presumably displaying that in a browser. It will LOOK like php code in a browser, because the browsers will render < as < but it's NOT PHP code. it's just some text.
PHP is not recursively executable, e.g.
<?php
echo "<?php echo 'foo '; ?>";
?>
would echo out <, ?, p, etc..., not just foo.
You can do stuff like
<?php
$foo = "<?php echo 'hello world!'; ?>";
file_put_contents('hello.php', $foo);
?>
without any issues. As long as the file you're producing actually gets executed by PHP (e.g. don't name it "hello.html"), then PHP will not know (or even care) that the script was produced by some OTHER php code.
You are encapsulating your variables in single-quotes, therefore they get output as plain text, simply remove the commas on any of your echo statements to fix this.
<?php echo $stfromyearErr; ?>
When you want to output PHP code including tag and even syntax highlighting, you can use the function highlight_string(). Documentation here
<?php
highlight_string("<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>");
?>
There are two things, first remove the quotes:
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='1' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo $stfromyearErr ?&gt&lt?php echo $stfrommonthErr
?&gt&lt?php echo $stfromdayErr ?&gt &lt?php echo $sttoyearErr ?
&gt&lt?php echo $sttomonthErr ?&gt&lt?php echo $sttodayErr ?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
However, this will give you:
<?php echo your_var_1 ?><?php echo your_var_2 ?><?php echo your_var_3 ?> <?php echo your_var_4 ? ><?php echo your_var_5 ?><?php echo your_var_6 ?>
This is because the php tags are redundant: you have already started php and are echoing these out within php.
If this is unavoidable, try replacing them like this:
echo str_replace(" ?&gt", "", str_replace("&lt?php echo", "", $page));

PHP echo inside echo

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 } ?>

Insert HTML in if statement

<?php
if(get_field('member_only_content')){
echo "do something";
}else{
echo "do something else";
}
?>
How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.
Close and open the PHP blocks, like this:
if(get_field('member_only_content')){
?>
<html></html>
<?php
} else{
?>
<html></html>
<?php
}
You can also use PHP's alternative syntax for this:
if(get_field('member_only_content')): ?>
<html></html>
<?php else: ?>
<html></html>
<?php endif;
Like this!
<?php
if(get_field('member_only_content')) : ?>
<div>Html stuff</div>
<?php else : ?>
<div>More Html stuff</div>
<?php endif;
Put HTML in place of the string.
<?php
if(get_field('member_only_content')){
echo "<your>HTML here</your>";
}else{
echo "<other>HTML</other>";
}
?>
You can also break out of the PHP tags
<?php
if(get_field('member_only_content')){
?>
<your>HTML here</your>
<?
}else{
?>
<other>HTML</other>
<?
}
?>
That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:
Echo the HTML
<?php
if (get_field ('member_only_content')) {
echo "<span>Put HTML here</span>";
}
else {
echo "<span>Put HTML here</span>";
}
?>
Place the HTML Outside PHP Tags
<?php if (get_field ('member_only_content')): ?>
<span>Put HTML here</span>
<?php else: ?>
<span>Put HTML here</span>
<?php endif;?>
Or you can use the <<< statement:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
Straight from http://php.net/manual/en/function.echo.php
<?php
if(get_field('member_only_content')){
echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
echo "do something else";
}
?>
Be careful while inserting quotes in your string.
Sample code
echo("<h1>This is a sample</h1>");

Categories