Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Are there any way to use this alternative syntax?
<div class="lines-rates">
<?php foreach($info as $k => $v){ echo : ?>
<div><span>{$k}</span>{$v}</div><?php ; } ?>
</div><!-- #lines -->
I mean:
<?php echo : ?><p>this is your {$username} And this html code could be more than one line and sure other html elements also</p><?php endecho; ?>
So we can easily see html code.
I want to have html inside PHP. Not PHP inside html.
<?php echo '<p>this is your '.$username.' And this html code could be more than one line and sure other html elements also</p>'; ?>
It's definitely not the way that I want.
I'm using
<?php if($info) : ?>
<div class="lines-rates">
<?php foreach($info as $k => $v){ echo "<div><span>{$k}</span>{$v}</div>"; } ?>
</div><!-- #lines -->
<?php endif; ?>
I do not want to use html inside php.. Like
<?php echo "<div><span>{$k}</span>{$v}</div>";?>
Also following line is possible but this is too verbose for me - is it not? Why do I have to write <?php echo $k; ?> to echo simple variable?
<?php $if($info) : ?>
<h2>Weekly Charter Rates </h2>
<div class="lines-rates">
<?php foreach($info as $k => $v) : ?>
<div><span><?php echo $k; ?></span><?php echo $k; ?></div>
<?php endforeach; ?>
</div><!-- #lines -->
<?php endif; ?>
I need clean and pure code as much as possible.
This type of writing again fails.
<?php foreach($info as $k => $v) : echo <<<EOT ?>
<p>this is your {$username}</p>
...
some more html
..<div><span>$k</span>$k</div>
<?php EOT; ?>
<?php endforeach; ?>
It was almost best way.
Use this:
<div class="lines-rates">
<?php foreach($info as $k => $v){?>
<div><span><?php echo $k; ?></span><?php echo $v; ?></div>
<?php } ?>
</div>
If you are using only two variable of PHP then no need to echo whole line, just echo those variables.
You can use Heredoc syntax
echo <<<EOT
<p>this is your {$username}</p>
...
some more html
..
EOT;
UPDATE
there must be no other content in the line of second "EOT". Quote from link I've given:
It is very important to note that the line with the closing identifier
must contain no other characters, except possibly a semicolon (;).
UPDATE 2
Change
<?php EOT; ?>
to
<?php
EOT;
?>
DO NOT IDENT SECOND LINE
You could use a templating language such as Twig. Example syntax:
<ul id="navigation">
{% for item in navigation %}
<li>{{ item.caption }}</li>
{% endfor %}
</ul>
what about this
<?php echo '<p>this is your '.$username.' And this html code could be more than one line and sure other html elements also</p>'; ?>
If you're feeling really adventurous you can choose to enable short tags, but both Heredoc and templating engines like Twig mentioned above would be a better idea.
<p>this is your <?=$username;?> And this html code could be more than one line and sure other html elements also</p>
You can also make use of nowdoc
echo <<<'END_OF_HTML'
$hello this is {$a->test}
END_OF_HTML;
OUTPUT:
$hello this is {$a->test}
Related
Ever since i've used CakePHP, I asked myself about the deeper sense of the recommended syntax of CTP-files, which is basically a HTML-file with all PHP code bracketed with tags. I find this very hard to read and I should think that the context switches between HTML and PHP would add some performance penalty.
Wouldn't it be faster and clearer to collect all output in a string and echo it at the end?
But there is some deeper sense for sure, just that i don't see it..
To make myself clearer, here's an example:
CakePHP:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
My approach:
<?php
$out = '';
if (!empty($file['User']['email'])) {
$out .= '<div class="mailto">'.$this->Html->link($file['User']).'</div>';
}
if (!empty($file['Document']['comments'])) {
$out .= '<div class="file-comment file-extra column grid_6">'
.'<div class="content">'.$file['Document']['comments'].'</div>'
.'</div>';
}
echo $out;
?>
So my question is: What are the drawbacks to my approach compared to CakePHP's ?
First things first: writing your entire template as PHP, then echoing it is not a great idea. As a general rule of thumb, I avoid echoing HTML from PHP ever, if I can. there are many reasons, but the main one will be the lack of syntax highlighting in your IDE.
Anyway, code formatting is entirely down to personal preference, but if you're writing your templates like this:
<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>
...it's no wonder you can't read them.
There are a few things you could try, to make your code clearer and easier to read. Again, these are down to your own personal preferences, and you could get into the habit of using some or all of them.
Format your HTML properly, with indentations for child elements.
Add white space between lines of code that are too busy, particularly between lines of PHP and lines of HTML.
Use short echo tags syntax (<?= instead of <?php echo).
Assign the more complex PHP values to variables so that your HTML is easier to read.
Remember to comment your code (HTML or PHP), particularly adding HTML comments so that you can easily see separate components of your template at a glance.
Example
<?php
$user = $file['User'];
$comments = $file['Document']['comments'];
?>
<!-- User -->
<?php if (!empty($user['email'])) : ?>
<div class="mailto"><?= $this->Html->link($user); ?></div>
<?php endif; ?>
<!-- File Comments -->
<?php if (!empty($comments)) : ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?= $comments; ?></div>
</div>
<?php endif; ?>
I was referring the page.tpl.php(Drupal 7 theme) for understanding the code. I found the following code,
<?php if ($site_name || $site_slogan): ?>
<!-- !Site name and Slogan -->
<div<?php print $hgroup_attributes; ?>>
<?php if ($site_name): ?>
<h1<?php print $site_name_attributes; ?>><?php print $site_name; ?></h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<h2<?php print $site_slogan_attributes; ?>><?php print $site_slogan; ?></h2>
<?php endif; ?>
</div>
<?php endif; ?>
Can you see the code in third line, <div<?php print $hgroup_attributes; ?>> WHY the php code is inside the first div tag of html? Same thing in later part of code also, as you can see h1 and h2 code. So, what is this convention of combining the html and php in so complicated way? and how should I read that?
Combining HTML and PHP code in Drupal templates is actually a very strong feature. In this case, $hgroup_attributes will probably contain some classes that style the div. Printing it in the template results in something like
<div class="SOME_CLASSES"> ... </div>
If you're further interested in the variable $hgroup_attributes, you can inspect by pasting <?php dpm($hgroup_attributes); ?> in your template file after you've installed the Devel module.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
currently I'm developing a application using Codeigniter framework and PHP. In there I need to include below mentioned PHP code in between HTML tags.
Can anyone provide a solution or an guidance for this?
PHP code :
if(isset($_SESSION['email']))
{
echo '<a href="http://localhost/ci/myads_view">';
echo 'MY ADS' ;
echo '</a>';
}
HTML code:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"/>
</nav>
You can come in and out of PHP at will:
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } ?>
In the above, if the PHP if statement evaluates to true, the HTML within the conditional will execute, otherwise it will not.
In your particular case, I can only assume this is what you're trying to do:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<li>
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } else { ?>
OTHER LINK
<?php } ?>
</li>
</ul>
</nav>
Or something along the lines of the above?
PHP is pretty cool, you can stop/resume it mid-document.
For example like this, I hope this is the solution to what you were trying to do:
<?php
if(isset($_SESSION['email']))
{ /* this will temporarily "stop" php --> */ ?>
<a href="http://localhost/ci/myads_view">
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
</a>
<?php /* <-- php resumes now */
}
?>
So what's going on?
First, you write your IF statement and the curly brackets, the code inside of which would usually be executed if the IF statement evaluates to TRUE.
<?php
if(something==true)
{
echo "write something to the page";
}
?>
But instead of using PHP commands, you quickly jump out of PHP by closing the PHP tag ?> as you would do at the end of your PHP code anyway.
<?php
if(something==true)
{
?>
Now you can continue in plain HTML without all the usual related issues of PHP commands as escaping characters like ' and " and \ watching your "qoute level" and so on. Instead, you write bog standard HTML.
When you're done entering the HTML that should be output in case the IF statement is true, just open another PHP tag and close the IF statement's curly brackets and, if that's the end of your PHP, close the PHP tag as well.
<?php
}
?>
What you have done is correct, additionally you can write as follows:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str;
Reference - http://php.net/manual/en/language.types.string.php
To put the link inside the navigation, you can do it like this, all inside the same PHP file.
<?php
if(isset($_SESSION['email'])) {
$navLink= 'MY ADS';
}
?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"><?php echo($navLink); ?>
This should work:
<?php if(isset($_SESSION['email'])): ?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<?php endif; ?>
Here I am writing the code fully to explain my problem. the links are generated but the list is not.
what could be the problem? thanks..
require 'database.php';
$query="SELECT subject.subjectName, subject.subjectId FROM course,subject where subject.subjectId=course.subjectId
and course.memberId=1";
$courses=$db->query($query);
?>
<!doctype html>
<html>
<head>
<title>foreach</title>
</head>
<body>
<?php foreach ($courses as $course):?>
<a href="#">
<?php echo $course['subjectName'];?>
</a>
<br>
<?php endforeach; ?>
<ul>
<?php foreach ($courses as $course):?>
<li>
<?php echo $course['subjectName'];?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Your second code has a quote inside before the closing PHP tag.
Before
<?php foreach($courses as $course):?> <a href"<?php echo $course['courseName'] "?> </li>
<?php endforeach;?>
After
<?php foreach($courses as $course):?> ???
<?php endforeach;?>
And for easier to read code
<?php
foreach($courses as $course) {
echo 'A Link';
}
?>
No, foreach does not modify the object. You are probably overriding $courses variable between two foreach blocks
Just not sure if its mistake while type code here or you have exactly same code in your script but pls check this in second for each
Here you are missing "=" after href should be like this
is missing
Did you check page source? It might be hidden by css as you have different HTML tags in each block
Hi everyone I finally got the answer for my problem and here it is:
In my code $courses is a PDOstatement Object and foreach loop calls upon fetch() method and fetch() method frees the result set, that means at end of foreach loop there are no more rows left. its all freed, so we can not use the same $courses again in loop since there is nothing left in it...
hope I am not missing anything ...
I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>