PHP multiline string with PHP - php

I need to echo a lot of PHP and HTML.
I already tried the obvious, but it's not working:
<?php echo '
<?php if ( has_post_thumbnail() ) { ?>
<div class="gridly-image"><?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) ));?>
</div>
<?php } ?>
<div class="date">
<span class="day">
<?php the_time('d') ?></span>
<div class="holder">
<span class="month">
<?php the_time('M') ?></span>
<span class="year">
<?php the_time('Y') ?></span>
</div>
</div>
<?php } ?>';
?>
How can I do it?

You don't need to output php tags:
<?php
if ( has_post_thumbnail() )
{
echo '<div class="gridly-image">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )) .'</div>';
}
echo '<div class="date">
<span class="day">'. the_time('d') .'</span>
<div class="holder">
<span class="month">'. the_time('M') .'</span>
<span class="year">'. the_time('Y') .'</span>
</div>
</div>';
?>

You cannot run PHP code within a string like that. It just doesn't work. As well, when you're "out" of PHP code (?>), any text outside of the PHP blocks is considered output anyway, so there's no need for the echo statement.
If you do need to do multiline output from with a chunk of PHP code, consider using a HEREDOC:
<?php
$var = 'Howdy';
echo <<<EOL
This is output
And this is a new line
blah blah blah and this following $var will actually say Howdy as well
and now the output ends
EOL;

Use Heredocs to output muli-line strings containing variables. The syntax is...
$string = <<<HEREDOC
string stuff here
HEREDOC;
The "HEREDOC" part is like the quotes, and can be anything you want. The end tag must be the only thing on it's line i.e. no whitespace before or after, and must end in a colon. For more info check out the manual.

Use the colon notation
Another option would be to use the if with a colon (:) and an endif instead of the brackets:
<?php if ( has_post_thumbnail() ): ?>
<div class="gridly-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )); ?>
</a>
</div>
<?php endif; ?>
<div class="date">
<span class="day"><?php the_time('d'); ?></span>
<div class="holder">
<span class="month"><?php the_time('M'); ?></span>
<span class="year"><?php the_time('Y'); ?></span>
</div>
</div>

The internal set of single quotes in your code is killing the string. Whenever you hit a single quote it ends the string and continues processing. You'll want something like:
$thisstring = 'this string is long \' in needs escaped single quotes or nothing will run';

To do that, you must remove all ' charachters in your string or use an escape character. Like:
<?php
echo '<?php
echo \'hello world\';
?>';
?>

Use the show_source(); function of PHP. Check for more details in show_source. This is a better method I guess.

Related

Add comma between foreach looped items but not at end

I'd like to achieve on adding a comma between my authors in my foreach but each time I make any references, it adds a comma at the end also.
Here is what I have below:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($recipe->get_authors() as $profile): ?>
<?= $profile->get_name() ?>,
<?php endforeach; ?>
<p><?= $recipe->get_content() ?></p>
</div>
Which gives me the following results:
How can I prevent a comma at the end?
Since you were skeptical about implode, here's a simple example (just showing the loop):
<?php foreach ($recipe->get_authors() as $profile):
$links[] = '' . $profile->get_name() . '';
endforeach; ?>
<?= implode(', ', $links); ?>
An alternative using join:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> - <?= join(", ", array_map(function ($profile) { return sprintf('%s', $profile->get_url(), $profile->get_name()); }, $recipe->get_authors())) ?>
<p><?= $recipe->get_content() ?></p>
</div>
Wasn't able to test it but basically store results to $authors first, and in this way you can count it without recalling the method get_authors. While $cnt keeps track base 1 and can be compared directly on the length.
<?php
$authors = $recipe->get_authors();
$cnt = 0;
?>
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($authors as $profile) { $cnt++; ?>
<?= $profile->get_name() ?><?php
if ($cnt < count($authors)) echo ",";
?>
<?php } ?>
<p><?= $recipe->get_content() ?></p>
</div>

Show PHP/HTML as text on page in php template

How can I spit out the following code as text on a page inside of 'pre' and 'code' tags without my php template actually trying to read the php code. With js and css the 'code' and 'pre' tags stop the actual code from running. How do I do this with the following combo of HTML/PHP.
I have tried using htmlescaping but this only spits out the php. I can't figure out what the right combination is here.
<pre>
<code>
<?php elseif(get_row_layout() == 'three_col_panel'): ?>
<!-- ==================================== -->
<!-- === 3 COLUMN ICON TEXT GRID ======== -->
<!-- ==================================== -->
<div class="icon-panel-three block">
<div class="main-wrap">
<div class="container">
<div class="row">
<?php if( have_rows('pillar')): ?>
<?php while ( have_rows('pillar')) : the_row() ?>
<div class="col-md-4 icon-column">
<div class="inner-content">
<?php $image = get_sub_field('image');
$url = $image['url'];
$alt = $image['alt'];
$size = 'medium';
$thumb = $image['sizes'][$size];
?>
<img src="<?= $thumb; ?>" alt="<?= $alt; ?>"/>
<div class="text">
<h1><?= get_sub_field('heading')?></h1>
<p><?= get_sub_field('paragraph')?></p>
</div><!--end text-->
<?php if( have_rows('column_link')): ?>
<?php while ( have_rows('column_link')) : the_row() ?>
<?= get_sub_field('col_link_text'); ?><img src="<?= get_template_directory_uri(); ?>/img/button-arrow.svg" alt="click here" />
<?php endwhile; ?>
<?php endif; ?>
</div><!--end inner-content-->
</div><!--end col-->
<?php endwhile; ?>
<?php endif; ?>
</div><!--end row-->
</div><!--end container-->
</div><!--end main-wrap-->
</div><!--end tile-panel-->
</code>
</pre>
Precondition:
Text to output must be in a variable string or in a separate file.
I suggest you the file solution: it is more simple, the code is more clear, and you can use one file to display more that one code.
If you prefer the string-way, you have to wrap it by single quotes and escape internal single quotes:
$string = '<?php elseif(get_row_layout() == \'three_col_panel\'): ?>
<!-- ==================================== -->
(...)';
You can't use double quotes, because they allow php variables evaluation.
If you prefer, you can use also Nowdoc string syntax: in this case, no escaping is needed.
Best way:
Use highlight_string( $string ) if your text is a string, or highlight_file( $filepath ) if you want output a complete file.
You don't need to wrap it by <code></code>: above commands wrap-it for you.
As gift, you will have colored syntax!
Alternative way:
For string:
<code><?php echo htmlentities( $string ); ?></code>
For files:
<code><?php echo htmlentities( file_get_contents( $filepath ) ); ?></code>
You just have to put all that between double quotes: "...", and escape the double quotes inside:
<div class=\"icon-panel-three block\">
<div class=\"main-wrap\">
.... etc
Another thing you can do is this: put it all in a .txt file and show the contents with php <?php
$homepage = file_get_contents('mycode.txt');
echo $homepage;
?>
with php you can try the php function "highlight_string();" like this:
<?php
highlight_string('<?php phpinfo(); ?>');
?>
It will output the ('input') as a string with colorcoding.
check out its php documentation: http://php.net/manual/en/function.highlight-string.php

Use of undefined constant

Hi I have this error in my website
my website is wordpress 4
i use post format
in post format i create this php to if page has quote post then dont load title and image and etc from quote post just show quote content is meta box (wordpress)
Notice: Use of undefined constant quote - assumed 'quote' in C:\xampp\htdocs\personal\wp-content\themes\personal\warp\systems\wordpress\layouts\_post.php on line 4
and
Notice: Use of undefined constant quote - assumed 'quote' in C:\xampp\htdocs\personal\wp-content\themes\personal\warp\systems\wordpress\layouts\_post.php on line 14
this is _post.php code in bellow
<?php $format = get_post_format(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('group'); ?>>
<?php
if ($format != quote ) {
$css_class = "b-box";
} else {
$css_class = "q-box";
}
echo "<div class=\"$css_class\">";
?>
<?php get_template_part('inc/post-formats'); ?>
<?php if ($format != quote ) : ?>
<div class="post-content">
<div class="blog-img-box">
<img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" />
</div>
<div class="post-deco blog-icon-box">
<div class="hex hex-small">
<i class="fa blog-icon"></i>
<?php if ( $format != false ) :?><?php endif; ?>
</div>
</div><!--/.post-deco-->
<h2 class="blog-title">
<?php the_title(); ?>
</h2><!--/.post-title-->
<div class="blog-details row">
<div class="col-md-4"><i class="fa fa-calendar blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php the_time('j M, Y'); ?></p>
</div>
<div class="col-md-4">
<a href="<?php comments_link(); ?>">
<i class="fa fa-comment blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php comments_number( '0', '1', '%' ); ?> Comments</p></a>
</div>
<div class="col-md-4"><i class="fa fa-eye blog-details-icons"></i>
<p class="blog-details-icons-txt"><?php echo getPostViews(get_the_ID()); ?></p>
</div>
<div class="likes"><i class="fa fa-heart blog-details-icons"></i>
<p class="blog-details-icons-txt">18</p>
</div>
</div>
<div class="blog-txt">
<?php the_excerpt(); ?>
</div>
<div class="readmore-box"><p class="readmore-txt"><?php _e('Read More','anew'); ?></p></div>
</div><!--/.post-content-->
<?php endif ?>
</div><!--/.post-inner-->
</article><!--/.post-->
please check and say to me what i wrong
thank you !
If you want to use the quote as a literal string (line 4), not a variable name, you must enclose it into single or double quote characters (' or "). Otherwise, PHP thinks that you're referencing a PHP constant.
For reasons of backward compatibility, PHP falls back on using the string as literal, but it does throw an E_NOTICE error – the one you're experiencing.
verify that all variables have this sign $
correct variable $test
incorrect variable test

substr not working with $item->getTitle

I'm making a website with Joomla and am using RokSprocket to display some news. Now, some Titles are too long, so I wanted to shorten it after a certain character number, but it's not working. Am a total php noob, my apologies.
Here's the whole code:
<li data-lists-item>
<h4 class="sprocket-lists-title ">
<?php if ($item->custom_can_have_link): ?><a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>"><?php endif; ?>
<?php if(strlen($item->getTitle)>10)
echo substr($item->getTitle,0,10) . ' ...';
else
echo $item->getTitle();?></a>
<div class="date"><?php echo date('d.M. Y', strtotime($item->getDate()));?></div>
</h4>
<span class="sprocket-lists-item" data-lists-content>
<span class="sprocket-padding">
<?php if ($item->getPrimaryImage()) :?>
<img src="<?php echo $item->getPrimaryImage()->getSource(); ?>" class="sprocket-lists-image" />
<?php endif; ?>
<?php echo $item->getText(); ?>
<?php if ($item->getPrimaryLink()) : ?>
<span><?php rc_e('READ_MORE'); ?></span>
<?php endif; ?>
</span>
</span>
</li>
I'm hoping it's something very obvious and I'm just being stupid. Thanks a bunch already!
$item->getTitle is probably a method not a property, so try:
$item->getTitle(); // <-- parenthesis
Change to:
<?php if(strlen($item->getTitle())>10)
echo substr($item->getTitle(),0,10) . ' ...';

php foreach generating trash

I have here a quite complicated loop what I would like to modify while is not really working as I expected. The problem is: my if statements else condition is generating trash and I don't really know where the problem comes
<div id="container">
<?php include 'header.php';
foreach ($vars as $item):
if (is_array($item['productItems'])):?>
<div class="product_box">
<h4>
<?php echo $item['title']?>
</h4>
<?php foreach($item['productItems'] as $products):?>
<?php foreach($products as $key=>$product):?>
<div class="product">
<div class="image_holder">
<a href="<?php echo $product['image']['medium'];?>" class="preview">
<img src="<?php echo $product['image']['small'];?>"
alt="gallery thumbnail" title="" />
</a>
<?php foreach($product['trackingLinks'] as $key=>$trackinglink): ?>
<a href="<?php echo $trackinglink[0]['ppc']?>"> <span><?php echo $product['name'];?>
</span>
</a>
</div>
<h5>
<?php echo 'Preise: '. $product['price'].' '.$product['currency'];?>
</h5>
<span><?php echo 'Versand Kosten: '.$product['shippingCosts'].' EUR';?>
</span>
<p>
<?php echo (substr($product['descriptionLong'], 0,150 )).'...mehr';?>
</p>
<a class="shopping_button"
href="<?php echo $trackinglink[0]['ppc']?>">zum Shop</a>
</div>
<?php endforeach ?>
<?php endforeach ?>
<?php endforeach ?>
<div class="ajax_bar">
<select id="ajax_call" size="1" name="Weitere Produkte">
<option value='50'>Weitere Produkte</option>
<option value='100'>weitere 100</option>
<option value='150'>weitere 150</option>
</select>
</div>
</div>
<?php else: ?>
<div class="text_box">
<h2>
<?php echo $item['title'];?>
</h2>
<p>
<?php echo $item['text'];?>
</p>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php include 'footer.php';?>
</div>
I'm going to make a wild guess here and say that this is because you don't escape your output using htmlspecialchars or htmlentities.
If you're not sure what's inside the for-loop, use this nifty construct to debug on page:
echo '<pre>', htmlspecialchars(print_r($vars, true)), '</pre>';
It also helps to look at the page source instead; this usually gives vital clues as to why your page layout is so messed up.
Update
Okay got it! It's because of this:
<?php else: ?>
<div class="text_box">
<h2>
<?php echo $item['title'];?>
</h2>
<p>
<?php echo $item['text'];?>
</p>
The $item is a string here, and $item['title'] is understood by PHP as $item[0] which is the first character of $item :)

Categories