This question already has answers here:
How to properly indent PHP/HTML mixed code? [closed]
(6 answers)
Closed 9 years ago.
Is there a PHP function/class that cleans my HTML code?
For example:
$html = "<ul><li>item1</li><li>item2</li>";
echo htmlClean($html);
/*
Outputs:
<ul>
<li>item1</li>
<li>item2</li>
</ul>
*/
tidy. or one of the third party php libraries
This might help: Indent HTML Code
keep html out of your php for a start, you should only echo out strings not blocks of html.
<ul>
<?php foreach($items as $item): ?>
<li><?php echo $item['one']; ?></li>
<?php endforeach; ?>
</ul>
If needs must you could try clean the code up with " Heredoc string quoting "
echo <<<EOF
<ul>
<li>"$item1"</li>
<li>"$item2"</li>
</ul>
EOF;
Related
Hey fellow developers!
I'm new to PHP and backend development so as I was learning, I was facing a little "issue", where I couldn't exactly realise the difference between the opening tags (<?php) and (<?=)
The main example I was facing was the below one:
<?php
$drinks = [
"cola" => 2,
"fanta" => 3,
"sprite" => 4
];
$pastries = [
"Croissant",
"Muffin",
"Slice of Pie",
"Slice of Cake",
"Cupcake",
"Brownie"
];
?>
<h1>Welcome to the Repetitive Cafe</h1>
<h3>Drinks!</h3>
<ul>
<?php foreach ($drinks as $drink => $price):?>
<li><?="$drink costs $price dollars"?></li>
<?php endforeach;?>
</ul>
<h3>Pastries! ($2 each)</h3>
<ul>
<?php for($i = 0;$i<count($pastries); $i++):?>
<li><?= $pastries[$i] ?></li>
<?php endfor; ?>
</ul>
My question is, what is the difference between those two and why will my code not run if I add instead of between the list elements in HTML?
Thanks in advance and Happy Coding!
<?php is the PHP code opening tag. <?= is the echo opening tag. Both tags are closed by ?>. The former can contain any PHP code. The latter can contain a single expression.
So this:
<?= $foo ?>
Is basically just a shortcut for this:
<?php echo $foo; ?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I am trying to add if statemenet into $html variable. But it see php as a html tag. how can add this statement. Thanks
$html = '
<?php if (x=y):?>
<div>
Help me )
</div>
<?php endif ?>
';
Try following
$html = '';
if(x==y):
$html .= '<div> Help me )</div>';
endif;
You can do without closing php tags.
Edited
After #MichałSkrzypek comment I have edited and fix mistake in if statement
What you are trying to do is impossible. If you would want to echo the var, you would put <?php inside of <?php, which must result in an error. Only add
<div>
Help me )
</div>
to a var.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I migrate my old app. On old server (php 5.2) I'm using nested alternative if statement to mix html and php code in view, like this:
<?php if($pg_id==1): ?>
<li <?php if ($nav2=='news'): echo 'class="active"'; endif; ?>>News</li>
<? endif; ?>
Why on the new server (php 5.5) the above example doesn't works?
I'm getting error:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\testapp\system\libraries\Loader.php(673) : eval()'d code on line 372
If I want this statement works, I need to rewrite it like this:
<?php if($pg_id==1):
echo '<li';
if ($nav2=='news')
echo 'class="active"';
echo '>News</li>';
endif; ?>
The app is written using Codeigniter 1.7.2.
Try it it will works... If not comment please
<?php if($pg_id==1):
if ($nav2=='news'):?>
<li class="active">News </li>
<?php else: ?>
<li class="">Title</li>
<?php endif;
endif;
?>
Rather than using above code use the Ternary Operator. It will be much easy and understandable.
<?php
$class= ($nav2=='news')?'active':'';
if($pg_id==1): ?>
<li class="<?php echo $class;?>">News</li>
<?php endif; ?>
Try it.. will be 100% works.
This is my first question.
I'm building a simple dynamic menu using <li>
I'm working on a PHP based CMS (Kirby)
Kirby has predefined PHP functions (helpers)
I'm trying to output a <li> for each page on the website:
<li class='active'><a href='page1'></a></li>
<li><a href='page2'></a></li>
<li><a href='page3'></a></li>
...
Im using a PHP function e($condition, $value) to style the menu item only if that page isOpen()
// I need help here
<?php
foreach ($pages->visible() as $p):
echo "<li" . e($p->isOpen(), ' class="active"') . "><a href='" . $p->url() . "'></a></li>";
endforeach;
?>
The function is working but the css part class="active" is printing OUTSIDE the <li> on the final code
class="active"
<li>...</li>
<li>...</li>
<li>...</li>
I had this previous code that worked fine, but since i'm using display: inline-block the menu had spaces betwes each block, since the following code was placing each <li>in a new line.
// This code works
<?php foreach($pages->visible() as $p): ?>
<li <?php e($p->isOpen(), ' class="active"') ?> ></li>
<?php endforeach ?>
The reason i'm rewrinting the code is to remove the white space between the inline: blockelements.
I'm failing to concatenate the string in a way that the function works and print its results inside the <li>tag.
I've searched here and also have read lots of Docs in php.net but nothing worked to me, I'm struggling with this for 2 days.
I'm expecting to learn better how and when to use concatenation and string operators.
The problem is that Kirby's e() function already has the echo in the routine, instead of just simply returning the value.
http://getkirby.com/docs/cheatsheet/helpers/e
If you were to change your output loop to something more like this, echoing separately, you'll get the results in the desired order:
foreach($pages->visible() as $p)
{
echo "<li";
e($p->isOpen(), ' class="active"');
echo "> and the rest of your line </li>";
}
That said, perhaps using e() isn't the most elegant in this case. Maybe try the r() function instead:
http://getkirby.com/docs/cheatsheet/helpers/r
foreach($pages->visible() as $p)
{
echo "<li ".r($p->isOpen(), ' class="active"').">more text</li>";
}
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.
I know this is going to be voted as a bad question but I always have trouble with these.
I am making a php navigation using arrays and my code keeps falling short, mainly in foreach statements. Hopefully if you look you can see where I am trying to go
<html>
<head>
<title>navigation</title>
<?php
$pages = array("index.html" => "Home");
?>
</head>
<body>
<ul>
<?php
foreach($pages as $link => $page){
echo '<li> $page </li>';
}
?>
</ul>
</body>
</html>
$link and $page aren't going to get parsed here, since they're within single quotes:
echo '<li> $page </li>';
Do this instead:
echo '<li> ' . $page . ' </li>';
http://php.net/manual/en/language.types.string.php
Change this:
echo '<li> $page </li>';
To this:
echo "<li> $page </li>";
And you should be good to go.
Check up on strings, particular interpolation
http://php.net/manual/en/language.types.string.php