Can someone explain this PHP code? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
<h1 class="site-title">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"
rel="home"><?php bloginfo( 'name' ); ?>
</a>
</h1>
if (is_category('Ponies')) { ?>
// overlay a pretty rainbow on the logo for the ponies category
<img id="rainbow"
src='<?php bloiginfo('template_directory');?>/img/rainbow.png"
alt="OMG! Ponies! " />
<?php } ?>
I'm having trouble matching the PHP tags. The comment for the code says "Now any time the category of the content is Ponies, your header also includes the rainbow.png." But it's clear how that is happening. The actual code is on p245 of WordPress Design and Developement by Williams. Thanks for putting another pair of eyes on it.

"If" is not inside <?php ... ?>. Must be:
<?php if (is_category('Ponies')) { ?>

I prefer to use <?php if (condition): ?> when there's HTML in-between.
But anyhow...
1) The if() statement needs to be inside php tags.
2) You don't need echo to retrieve the bloginfo.
bloginfo() documentation
3) You've misspelled bloginfo at the bottom...
My code:
<h1 class="site-title">
<a href="<?php echo esc_url(home_url('/')); ?>" rel="home">
<?php $bloginfo('name'); ?>
</a>
</h1>
<?php if (is_category('Ponies')) : ?>
<img id="rainbow"
src="<?php get_bloginfo('template_directory') . '/img/rainbow.png'; ?>"
alt="OMG! Ponies!" />
<?php endif; ?>

Related

Cannot load a base64 encoded image from mysql database using php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have a longblob field in mysql database that stores a binary value for a image that i uploaded through my webpage.
I am trying to get the image using
<?php while( $record = mysqli_fetch_assoc( $result ) ): ?>
<div>
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($record['image']).'"height="300" width="300"/>'?>
<br/>
<?php echo "Title:".htmlentities( $record['title'] ); ?>
<br/>
<?php echo "Genre:".htmlentities($record['genre']); ?>
<br/>
<?php echo "Author:".htmlentities( $record['author'] ); ?>
<br/>
Edit</i>
<br/>
<a href="books.php?delete=<?php echo $record['id']; ?>"
onclick="javascript:confirm('Are you sure you want to delete this project?');" style="color:#1217b3">Delete</i></a>
</div>
<?php endwhile; ?>
and img tag looks like this in html
<img src="data:image/jpeg;base64,MlN0YXRlLmpwZw==" height="300" width="300">
You have base64 encoded the image file name not the file contents
MlN0YXRlLmpwZw==
actually outputs:
2State.jpg
When storing it in the database you need to do
$imageContent = base64_encode(file_get_contents($file));

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\eifel\title_bar.php on line 4 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a development server for PHP that runs XAMPP. The problem that I have is a parse error. The exact parse error that XAMPP is giving is:
"Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\eifel\title_bar.php on line 4"
I don't see any missing curly braces, nor semi-colons; but there is something that Notepad++ is giving me. There are wavy red lines under the file extension '.php'. The code for the class is below:
The code for the 'title_bar.php' class:
<div>
<?php
if(loggedin()){
<a href='index.php'>Home</a>
<a href='messages.php'>Messages</a>
<a href='logout.php'>Log Out</a>
}else{
echo "Not Logged In";
}
}
<a href='index.php'>Home</a>
<a href='login.php'>Login</a>
<a href='register.php'>Register</a>
</div>
Well that's not PHP. You have PHP and HTML and only PHP code should be between <?php and ?> tags:
<?php
if(loggedin()){
?>
<a href='index.php'>Home</a>
<a href='messages.php'>Messages</a>
<a href='logout.php'>Log Out</a>
<?php
}else{
echo "Not Logged In";
}
?>
<a href='index.php'>Home</a>
<a href='login.php'>Login</a>
<a href='register.php'>Register</a>
</div>
Or similar to what has already been said, you could use:
<div>
<?php
if(loggedin()){
echo "<a href='index.php'>Home</a>
<a href='messages.php'>Messages</a>
<a href='logout.php'>Log Out</a>";
}else{
echo "Not Logged In";
echo "<a href='index.php'>Home</a>
<a href='login.php'>Login</a>
<a href='register.php'>Register</a>";
}
?>
</div>
Make sure you echo or print any HTML elements in PHP otherwise you will get errors as HTML is not PHP.

Code not working in PHP if else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to create a nav bar where if you are a guest it displays a lock icon and 'Log in' and when you are logged in it displays a lock icon and 'Logout'.
Here is the code for my header:
<section class="top-bar-section">
<ul class="left">
<li><img src="images/icons/home_icon.png"> Home</li>
<li><img src="images/icons/about_icon.png"> About</li>
<li><img src="images/icons/pages_icon.png"> Showcase</li>
<li><img src="images/icons/videos_icon.png"> Videos</li>
</ul>
<ul class="right">
<?php
if (!empty($UserName))
{
echo <li><img src="../images/icons/login_icon.png"> Log in</li>;
}
else
{
echo <li><img src="../images/icons/login_icon.png"> Logout</li>;
}
?>
</ul>
</section>
the section I am working on is under <ul class="right">
and the error I am getting when testing the webpage is
`Parse error: syntax error, unexpected '<' in
C:\wamp\www...\header.php on line 16
PS: Line 16 is where the first 'echo' is
HELP! Please :) Thanks
You need to put the string in the echo statement in quotes (I'd suggest single-quotes, since you've got doubles in the text).
try like this you are missing quote on echo
if (!empty($UserName)) {
echo '<li><img src="../images/icons/login_icon.png"> Log in</li>';
}
else {
echo '<li><img src="../images/icons/login_icon.png"> Logout</li>';
}
The echo requires string like this
echo "Hello World";
So your echo statements must be like this:
echo '<li><img src="../images/icons/login_icon.png"> Log in</li>';
echo '<li><img src="../images/icons/login_icon.png"> Logout</li>';
If you want to use double quotes you should escape quotes that are in the string
echo "\""; /* would echo " */

Unexpected T_ECHO, expecting ',' or ';' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So, I've got this bit of code, for which I'm getting the following error.
Parse error, unexpected T_STRING, expecting ',' or ';'
I'm sure this is a very, very simple fix, but I'm still a bit new to the world of PHP. Any thoughts?
echo '
<li>
<a href="'.get_permalink().'">
<img src="'echo get_post_meta(get_the_ID(), 'video_tour_url', true);'">
<div class="galDiv">
<div class="boatTitle">'.get_the_title().'</div>
<div class="boatPrice">'.currency ().$price.'</div>
<div class="boatPower"> '.get_post_meta(get_the_ID(), '_map_ar_address', true).'</div>
</div>
</a>
</li>';
Your problem is with the following code:
<img src="'echo get_post_meta(get_the_ID(), 'video_tour_url', true);'">
Like the rest of your code it should use the concantination operator . and doesn't need an echo statement.
<img src="'.get_post_meta(get_the_ID(), 'video_tour_url', true).'">
You need a period in this code on the third line. and remove the echo:
<img src="'.get_post_meta(get_the_ID()
^
Don't echo HTML. Instead use inline PHP if possible.
<li>
<a href="<?php print get_permalink(); ?>">
<img src="<?php print get_post_meta(get_the_ID(), 'video_tour_url', true); ?>">
<div class="galDiv">
<div class="boatTitle"><?php print get_the_title(); ?></div>
<div class="boatPrice"><?php print currency() . $price; ?></div>
<div class="boatPower"><?php print get_post_meta(get_the_ID(), '_map_ar_address', true); ?></div>
</div>
</a>
</li>
looping? sure.
<?php foreach($widgets as $widget): ?>
<?php endforeach; ?>

PHP nest variable in echoed string that contains a HTML tag in the end [closed]

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
need php help
I need to add the if statement inside the html tag
if( get_field($image) ):
?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;
I want to add the if statement inside the html tag below in place of img and a, is that possible?
echo
"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">
// I want this if statement to work inside here...how do i escape the html to make it work?
if( get_field($image) ):
?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;
<img src=\"" .get_field($image)."\" alt=\"\" />
Download File
</$html>\n";
Putting PHP operators in HTML
First off, don't use the echo statement to spit out huge chunks of HTML, it makes code very hard to maintain and re-use. Isn't this easier?
<a href='<?php echo $some_variable; ?>'>
Using PHP logic in HTML blocks (general)
You're looking for something like this:
<?php if(!empty($image)): ?>
<img src='<?php echo $image; ?>' alt='Some Stuff'>
<?php endif; ?>
This is a short-hand equivelant called a ternary operator which may be easier to read in code:
<?php echo empty($image) ? null : "<img src='$image' alt='Some Stuff'>"; ?>
This will echo an image tag if $image has a value, and nothing if it doesn't.
Let's clean up & fix the code in the original post...
Your code looks like it has been deliberately obfuscated to confuse people. Learn to indent, don't embed logic within logic. Prioritize readability and your code will be a lot easier to maintain.
if(!empty($text))
echo
"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">
<img src=\"" .get_field($image)."\" alt=\"\" /> " .get_field($text)."
Download File
</$html>\n";
There is a lot that can be improved here. First of all, separate business logic out from display logic as much as possible:
Business logic
<?php
// This should be in another FILE ideally...
$this->divClass = empty($this->options['class']) ? null : trim($thesis->api->esc($this->options['class']));
$this->image = the_field($image);
$this->download = the_field($download);
$this->text = // I dont know how you're setting this.
?>
Display logic
Next, lose the get_field functions, add a null return to the_field if it's not found, that way you have cleaner code. Then, just use something like this:
<?php if(!isset($this->text)): ?>
<div class='<?php echo $divClass; ?>'>
<?php if(!isset($this->image) && !isset($this->download)): ?>
<img src='<?php echo $this->image; ?>'>
<a href='<?php echo $this->download; ?>'>Download File</a>
<?php endif; ?>
</div>
<?php endif; ?>
The <?php> tags are there to help you, they allow you to cleanly interpolate PHP code with HTML code in a way that most languages have to resort to ugly external tempating for. Use them, keep your code readable and understandable, don't take shortcuts because they will come back to bite you.

Categories