I am a little new to PHP. What I am trying to do is to display the username that is logged in with a link to their home page. This is what I have.
if (loggedin()) {
echo ' $user->get_fullname($uid) ' ;
echo ' Log Out ';
}
But as you can see it will not display the username, it will display what I have in between the link. I do not know where to go from here.
you want:
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {
?>
in php single quotes will not parse a variable. An alternative syntax is:
echo "<a href ='HomePage.php'> $user->get_fullname($uid) </a>" ;
This is because variables are interpreted inside of double quotes, not single quotes. There are many ways to fix this such as bringing the variable outside of the single quotes as in:
echo ''. $user->get_fullname($uid). '';
or to replace the single quotes with double quotes and vice-versa, and because it is a complex variable you will need to use braces:
echo "<a href ='HomePage.php'>{$user->get_fullname($uid)}</a>";
Use double quotes all throughout and escape the inner quotes:
echo "{$user->get_fullname($uid)}";
And lastly, my favourite way if I have a lot of HTML code is to use HEREDOC syntax:
if(loggedin()){
echo <<<HTML
{$user->get_fullname($uid)}
Log Out
HTML;
}
?>
Note: When using HEREDOC notation, you cannot put anything after the HEREDOC opening variable (not even space) and nothing else (not even space) on the closing HEREDOC line. That is why the closing HTML; is not indented here as no space is allowed before it.
You have to end the string with the quote and continue it to the php variable with the . operator. You concatenate strings with php variables using the .
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {
//do something else
}
if(loggedin()){
$userName = $user->get_fullname($uid);
if($userName == NULL) $userName = 'USER';
if(loggedin()){
echo "<a href ='HomePage.php'>".$userName."</a>";
echo " || <a href='logout.php'> Log Out </a>";
}else{
//if any other then you can...
}
If you want to display some variables in php it should not be enclosed in quotes but if you want a string to display you want it to be inside quotes. So in your case you have both so you must use something called concatenation, For which you use dot(.)[ In case of javascript you use plus(+) for concatenation].
So your code must be like
if(loggedin()){
echo ''. $user->get_fullname($uid) .'' ;
echo ' Log Out ';
}
else {
Related
The following line of code is supposed to echo the current season and a data in a php echo:
<?php echo "<h5>" $_SESSION['username'] '<span class="chat_date">'Dec 25"</span></h5>" ?>
Is the session call true? And how can I fix this code?
if you want echo a string you should use concatenation operator ('.')
<?php echo '<h5>' .$_SESSION['username']. '<span class="chat_date">Dec 25</span></h5>' ?>
or use double-quotes (") to passing data
$usename=$_SESSION['username'];
<?php echo '<h5>"$usename"<span class="chat_date">Dec 25</span></h5>' ?>
There is no need to concatenate a string before echoing it in PHP.
The parameters can be passed individually to echo as multiple arguments for a slight performance (speed) increase. In other words, you can use , instead of ..
For example:
<?php echo '<h5>' , $_SESSION['username'] , '<span class="chat_date">Dec 25</span</h5>'; ?>
I would recommend reading the Official Documentation.
<?php
echo "<h5> $_SESSION[username] <span class=\"chat_date\">Dec 25</span></h5>";
?>
Variables are evaluated inside double quoted strings.
To echo double quotes inside double quotes, you add a backslash before the double quote so PHP knows it is not the end of the quoted segment.
For arrays, do not quote the key inside a string.
Here is the code I wrote. The else is working. The if URL's are not.
<?php
if ( is_user_logged_in() ) {
echo'Set As Facebook Cover';
echo'Download';
} else {
echo'Set As Facebook Cover';
echo 'Download';
}
?>
To include a variable in a string, the string needs to be wrapped in double quotes, like
echo "This is a $var";
not echo 'This is a $var';
Or you can use the . to concat the strings:
echo 'This is a ' . $var;
Try this:
echo'Set As Facebook Cover';
echo'Download';
Final note, there is no need to use the ; after the variables because you are not ending the statement.
See a demo
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
Ive started on part of my new year resolution and decided to learn php, as part of it im trying to parse in an xml feed, and echo out the name of the events wrapped in <a> tags linking them back to the events page on the xml feed's site.
I think ive got it all in but i cant seem to see why this isnt working im just getting a blank page, if some one could point me in the right direction it would be much appreciated, cheers
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href="$event_attributes['url']">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
</ul>
echo "<a href="$event_attributes['url']">";
try changing that line to
echo "<a href=\"".$event_attributes['url']."\">";
The Php parser is pretty funny about this. Usually you pick one and just stick to it, or use both single quotes and double quotes as you please. Just remember that strings with double quotes are parsed for variables.
$hello = "Hello";
echo "$hello master";
is the same as
$hello ="Hello";
echo $hello.' master';
When you are testing your PHP scripts, you'll find it useful to switch on errors - then PHP will actually tell you why it isn't showing you anything:
error_reporting(E_ALL);
Normally you will have missed a ; or mis-typed a variable name.
in your case the error is here:
echo "<a href="$event_attributes['url']">";
You have accidentally ended the string with a double quote, so PHP thinks the string ends here:
echo "<a href="
This is where using single-quotes can be very handy because your double quotes won't then close the string.
echo '<a href="' . $event_attributes['url'] . '">';
The main difference between single and double quotes in PHP is that double quotes has special clever parsing rules and single quotes doesn't. For example:
$myVar = "BLAH";
echo "Example $myVar"; // Example BLAH
echo 'Example $myVar'; // Example $myVar
In your unordered list, you should use a dot to concatenate your string, and escape your double quotes like this:
echo "<a href=\"".$event_attributes['url']."\">";
Instead of
echo "<a href="$event_attributes['url']">";
Your example throws and error because you haven't used proper string concatenation. However, even with correct concat, it would render as <a href=http://someurl>, and you'd need to add the double quotes according to html standard. Hence you have to double quote.
if you want to not be troubled by having to switch between using a ' or a " then i suggest using the php alternative syntax php alternative syntax
with the given code it would look like
<?php
// F1 W/H xml feed
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<h2>".$type_attrib['name']."</h2>"; //Title - in this case f1 championship
} ?>
<ul>
<?php foreach($type->market as $event):?>
<li>
<a href="<?php echo $event_attributes['url']; ?>">
<?php echo $event_attributes['name']; ?>
</a>
</li>
<? endforeach;?>
</ul>
one advantage this would bring is that it would produce cleaner code since you can clearly distiguish your php code from your html which is the presentational part at the price writing all those other <?php ?> and as what others would claim a performance degradation. the choice is yours
Change
echo "<a href="$event_attributes['url']">";
for
echo "<a href=".$event_attributes['url'].">";
You are missing the periods in your second echo, where you have your $event_attributes['url']
<?php
foreach($type->market as $event) {
echo "<li>";
echo "<a href=".$event_attributes['url'].">";
echo $event_attributes['name'];
echo "</a>";
echo "</li>";
}
?>
I would recommend you to enable your error log, it would allow you to know the line with problems in any of your scripts.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating javscript function to destroy php session
Hi I am trying to create a php variable that will display itself as a popup. Here is the code I have...
<?php
// this starts the session
session_start();
$var = "";
// echo variable from the session, we set this on our other page
if ($_SESSION['color'] == "") {
$var = "<a href='JavaScript:newPopup('http://www.yourfantasyfootballreality.com/signin.php');' class='two'>Sign In</a>";
} else {
echo "Hello, ";
}
echo $var;
?>
I can't seem to arrange the semicolons and quotes correctly. Can someone please show me how this is done.
You can escape the quote character you are using to delimit your string within the string with the escape character (\)...
$var = "Sign In";
Alternatively, you can mix quotes, i.e. use single quotes to delimit your string and double quotes for quotes around your attributes or vice versa.
You could also use heredoc or nowdoc.
In "<a href='JavaScript:newPopup('http://www.yourfantasyfootballreality.com/signin.php');' class='two'>Sign In</a>"; the 2nd ' closes the href tag. You need to escape it, so you would do something like:
$var = "<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
So that when it actually outputs you get <a href='JavaScript:newPopup("http://www.yourfantasyfootballreality.com/signin.php");' class='two'>Sign In</a>
It's probably stupid question, but I can not find an answer. How can I style echo output with css? I have this code:
echo "<div id="errormsg"> Error </div>";
Now it displays syntax error, I think because of those quotes around errormsg. I've tried single quotes, but with no effect. Thank you
When outputting HTML, it's easier to use single quotes so you can use proper double quotes inside like so:
echo '<div id="errormsg"> Error </div>';
That will get rid of your parse error... To edit the style you will need to use CSS with the selector of #errormsg like so:
#errormsg {
color: red;
}
try
echo "<div id=\"errormsg\"> Error </div>";
First you need to either use single-quotes to surround the attribute value:
echo "<div id='errormsg'> Error </div>";
Or you could reverse that, to give:
echo '<div id="errormsg"> Error </div>';
Or you should escape the quotes:
echo "<div id=\"errormsg\"> Error </div>";
And then style the resulting element with the CSS:
#errormsg {
/* css */
}
The syntax problem you were encountering is a result of terminating the string and then having a disparate element between the first and second strings, with which PHP has no idea what to do.
To put double quotes inside of a double-quoted string, you need to "escape" them by putting blackslashes before them:
echo "<div id=\"errormsg\"> Error </div>";
In this case, another choice is to use single quotes for one or the other.
echo "<div id='errormsg'> Error </div>";
echo '<div id="errormsg"> Error </div>';
PHP's documentation has a section explaining the different string syntaxes, which should explain everything you could want to know about this subject.
Use single quotes around errormsg and what you have should work just fine. Alternatively, but less tidy, you can escape the double quotes with a backslash.
echo "<div id='errormsg'> Error </div>";
You are getting a syntax error because you are including unescaped double quotes inside a string that is delimited by double quotes.
Either escape them
echo "<div id=\"errormsg\"> Error </div>";
or use single quotes
echo '<div id="errormsg"> Error </div>';
The browser doesn't care if you generated markup using echo or something else. It just sees the HTML you send to it.
For the above markup, you can style it using an id selector:
#errormsg { /* … */ }
The usual rules for the cascade (including specificity) will apply.
If you don't want to care about single quotes or double quotes then the better way to achieve your answer is to use heredoc syntax .
Your solution :
<?php
$heredoc = <<< EOT
<div id="errormsg">Error solved</div>
EOT;
echo "$heredoc";
?>
css :
#errormsg{color: green;}
WARNING :
Do not add whiteSpace after <<< EOT
Do not add whiteSpace before EOT;
Do not add whiteSpace between EOT and ;
Do not add whiteSpace after EOT;
EOT; must be in new line.