Code not working in PHP if else statement [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 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 " */

Related

PHP - Changing HTML text with variable from MYSQL database [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 6 years ago.
Improve this question
I'm working on a website and I've got up to a bit now where I want text on the homepage to change, depending on whether or not the user is logged in. Before doing this, I wrapped the text in a div class, which has allowed me to configure how it looks inside a css file. By default, if the user is not logged in, the index.html should display "iBPBuyer, Amazing Deals & Prices." and when the user is logged in, it should then change that text to a simple welcome message, stating their name afterwards, but I cannot seem to get it working.
<?php
require("includes/application_top.php");
require("includes/site_header.php");
require("includes/application_bottom.php");
if($_SESSION['id']) {
echo '<div class="hero-text-box">
<h1>Welcome,<br> $_SESSION['first'].</h1>
</div>
<div class="row">
</div>';
} else {
echo '<div class="hero-text-box">
<h1>iBPBuyer,<br> Amazing Deals & Prices.</h1>
</div>
<div class="row">
</div>';
}
?>
Logged part should be:
echo '<div class="hero-text-box">
<h1>Welcome,<br>' . $_SESSION['first'] . '</h1>
</div>
<div class="row">
</div>';
So, it's just correct concatenation technique.
Simplify your code
$welcome = "iBPBuyer,<br> Amazing Deals & Prices.";
if($_SESSION['id']) {
$welcome = 'Welcome,<br> ' . $_SESSION['first'];
}
echo '<div class="hero-text-box">
<h1>' . $welcome . '</h1>
</div>
<div class="row">
</div>';

PHP If Else with Navigation Menu [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 6 years ago.
Improve this question
I am trying to create an if else statement where if the "if" part is true, a menu icon is displayed that says "Registration" and takes the user to a registration page. If the "else" part is true, the menu will display the user's name that is registered. I have attached basically how I would like it to look, I just do not know how to incorporate my div into a PHP if else statement.
<?PHP
session_start();
if ($_SESSION['email'] == '')
{
"<div id='cssmenu'>
<ul>
<li>
Registration
</li>
</ul>
</div>"
}
else
{
"<div id='cssmenu'>
<ul>
<li>
<a> "Greetings " . $_SESSION['fname'] </a>
</li>
</ul>
</div>"
}
?>
The following should do the trick. As mentioned by #Ghost, you need echo and you needed to change a few quotes to single quotes.
<?PHP
session_start();
if ($_SESSION['email'] == '')
{
echo "<div id='cssmenu'>
<ul>
<li>
<a href='registration.php'> Registration</a>
</li>
</ul>
</div>";
}
else
{
echo "<div id='cssmenu'>
<ul>
<li>
Greetings " . $_SESSION['fname'] . " </a>
</li>
</ul>
</div>";
}
?>
you can use php whitin html
the php code is only considered between php tags so anything else is put directly to browser
as i can see only one part of your html changes so a better design would be
<?php session_start(); ?>
<div id="cssmenu">
<ul>
<li>
<?php if ($_Session['email']=='') {
?>
Registration
<?php }else{ ?>
Greetings -> <?php echo $_SESSION['fname']; ?>
<?php }?>
</li>
</ul>
</div>

Can someone explain this PHP code? [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 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; ?>

Error code in 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 8 years ago.
Improve this question
So on my website im making it where if a member is not sign in they can't see username My account or settings link
this is what i have so far
<?php
if(empty($_SESSION['user'])){
echo "<li>
Register Account
</li>
<li>
Login Account
</li>";
} else {
echo "<li class='dropdown'>";
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'><?php echo $_SESSION['user']['username'];?></span>
<b class='caret'></b>
</a>";
echo "<ul class='dropdown-menu'>
<li><i class='icon-user'></i> My Profile</li>
<li><i class='icon-tasks'></i> My Tasks</li>
<li><i class='icon-calendar'></i> Calendar</li>
<li class='divider'></li>
<li><i class='icon-key'></i> Log Out</li>
</ul>
</li>";
}
?>
This is the error code i receive
Error i get is Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/index.php on line 92
You have a echo statement in another echo statement!
So change this:
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'><?php echo $_SESSION['user']['username'];?></span>
<b class='caret'></b>
</a>";
To this:
echo "<a class='dropdown-toggle' data-toggle='dropdown'>
<span class='username'>" . $_SESSION['user']['username'] . "</span>
<b class='caret'></b>
</a>";

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.

Categories