PHP - if something is equal to something then echo something - php

I'm quite new to PHP now that I've started working with wordpress.
I'm trying to get something to work by using 'if'
Essentialy what I'm wanting to do is
If the status is equal to Open then return Link
If the status is not equal to Open then return Link
Here's what I think would work:
<?php
if ($status) == (open) {
echo "id=flashing"
}
?>
Obviosuly, I'm assuming this doesn't work but what I'm wanting to do is create a link
Any help?

This is a really basic PHP syntax question; please read some documentation, and look at some examples before asking for help with every piece of code you write.
There is a comprehensive online manual for PHP, with many examples. It is available in multiple translations, in case English is not your first language.
Things you have wrong in your example, with links to relevant pages of the manual:
no semi-colon to end the statement
no quotes around the string open
brackets in the wrong place in the if statement
The text of your question also confuses return and echo, which have very different meanings.

Does this help?
if ($status == 'open') {
echo 'Link';
} else {
echo '<a href="#" >Link</a>';
}

Just use the following code:
<?php
if ($status == 'Open') {
echo 'Link';
}
else {
echo '<a href="#" >Link</a>';
}
?>

<?php
if ($status == 'open')
{
echo 'Link';
}
else
{
echo '<a href="link" >Link</a>';
}
?>

Assuming Open is a string, it can be written like this (alternatively to the other answers):
echo '<a href="#" '.(($status == 'Open') ? 'id="flashing"':'').'>Link</a>';

Related

Advise on simplifying a PHP statement

I am new to PHP and am coding a template file for a Joomla K2 item layout.
I have an 'extra field' $extrafields[15] configured which outputs as "Yes", "No" or "". $extrafields[16] is a text string.
I have this code, which works but would appriciate advice on how to simplify it, as I know it is probably a bit crude!
if (!empty($extrafields[15])):
if ($extrafields[15] == "Yes") {
echo "<span class=sgl-bold>Sponsored by: </span>";
}
if ($extrafields[15] == "Yes"):
if (!empty($extrafields[16])):
echo $extrafields[16];
endif;
echo "<br>";
endif;
endif;
I'd be inclined to do something like this:
if(!empty($extrafields[15]) && !empty($extrafields[16])){
if($extrafields[15] == "Yes"){
echo "<span class=sgl-bold>Sponsored by: </span>";
echo $extrafields[16];
echo "<br>";
} //endif not empty
} //endif yes
You can make your code more concise with just some simple tweaks:
Get rid of redundant if-clauses where possible,
combine if-clause conditions where it makes sense, and
don't use the alternative syntax for your control structures to cut down noise.
The following snippet preserves the same functionality than your original attempt but is imho more understandable.
if (!empty($extrafields[15]) && 'Yes' === $extrafields[15]) {
echo '<span class=sgl-bold>Sponsored by: </span>';
if (!empty($extrafields[16])) {
echo $extrafields[16];
}
echo '<br>';
}
That said, judging from the context, you probably want to go with the solution BigScar posted here.
To make this snippet even more understandable, you should consider working on the data structure (though I assume that this is something forced upon you by Joomla):
instead of a values in a numeric array like $extrafields[16] use speaking variable names like $showSponsor or the likes, and
instead of the string values of 'Yes', 'No' and '' use boolean values true, false and null.
Remember:
There are only two hard things in Computer Science: cache invalidation and naming things.
if (#$extrafields[15] == "Yes") {
echo "<span class=sgl-bold>Sponsored by: </span>";
echo #$extrafields[16];
echo "<br>";
}

Site prints all PHP instead of using it

another problem. I'm making a CMS and I want the login link to disapear when I want to. So I can configure it in my adminCP. When I "flip the switch" my configfunctions.php changes $login to true.
To use it I'm doing:
<?php if($loginenabled = true) { '<li><span class="mmLogin">Login</span></li>' }; ?>
but my site literally prints ALL PHP, also when I do other PHP. So my site looks like:
Home, About us, <?php if($loginenabled = true) { 'login' }; ?>
Does anyone know how to solve it?
Wesley
You can't output HTML code by simply creating a string. To write output, you can use the echo statement:
<?php if ($loginEnabled == true) {
echo '<li><span class="mmLogin">Login</span></li>';
}; ?>
Note the semicolon at the end of the single quotes ('). You can also use PHP's alternate syntax for control structures for cleaner code:
<?php if ($loginEnabled == true): ?>
<li><a href="sample-login.html" class="login">
<span class="mmLogin">Login</span>
</a></li>
<?php endif; ?>
The above will accomplish the same thing. Also note that ($loginEnabled = true) is always true, since = is an assignment operator. To check if $loginEnabled is true, use:
if ($loginEnabled == true)
You can also shorten it to simply:
if ($loginEnabled)
Read more on assignment operators, comparison operators, and the if statement.

Show Different "Code/Words" Based on URL Referer

I'm trying to have my website not show certain code if they come from certain URL's.
For example, Wikipedia don't like links to sites that have popups on them. So I need to not show the code for that referer.
I found the following code but it doesn't seem to work when code is places instead of text
<?php $ref=getenv('HTTP_REFERER');
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>
If you want to avoid showing code to google:
<?php if (!strstr(strtolower($_SERVER['HTTP_USER_AGENT']),"googlebot")){ ?>
//Show what you want, google will not see it
}else{
//show other code
}?>?>
For wikipedia:
<?php if (!strstr(strtolower($_SERVER['HTTP_REFERER']),"wikipedia")){ ?>
//Show what you want, wikipedia will not see it
}else{
//show other code
}?>
Enjoy ;)
You were talking about Wikipedia. Probably the problem is that google isn't sending you "google" in their referer string
it must work, try alternative variable
<?php
$ref=$_SERVER['HTTP_REFERER'];
if (strpos($ref,"google.com")>0)
{
echo "google";
}
else
{
echo "something else";
};
?>

Displaying an item depending on user login

This is the situation:
I have a list of items, and one of LI is:
echo '<li>Link</li>';
Now, I want to make an if statement here - if user is logged in, give home.php, else give index.php - but I'm kinda lost in all those "s and 's and .s so I'm asking for your help
This code won't do :/
echo '<li>Link</li>';
Also, I know I could do it with this code, but I want to finally get those dots and stuff
if ($logged == 0)
{
echo '<li>Link</li>';
}
else
{
echo '<li>Link</li>';
}
<?php echo '<li>Link</li>'; ?>
If you are after a short and concise solution try this:
<li>Link</li>
This is called a ternary operator. If $logged evaluates to true it will print 'home.php', otherwise it will print 'index.php'.
Here's an equivalent in standard if-else notation:
<li>Link</li>
You cannot use if's and echo's in echo parameters.
echo '<li>Link</li>';

calling function on hyperlink onclick event in php

can anybody help me here please?
I am using AJAX for pagination in my application. So I am generating hyperlinks with for loop. as follow:
for($t=1; $t<=$hf; $t++)
{
if($t == $_GET['pageno'])
{
echo $t." ";
}
else
{
echo "<a id ='$t' href='javascript:void(0)' onclick='open_page('ajaxinfo.php','content'); javascript:change('$t');'>$t</a>"." ";
}
}
Above echo statement does not give call to function. But instead of this when i just write html hyperlink it works fine and I get to see page2.html, my HTML code is:
<a id="page2" href="javascript:void(0)" onclick="open_page('ajaxinfo.php','content'); javascript:change('page2');">page2</a>
I don't understand why this is so? But is there any problem in echo's quotes.
Please Help.
that because you have syntax error while building anchors. Try to use double quotes for tag attributes and escape them with backslash.
So, your ECHO should look like this:
echo "<a id =\"{$t}\" href=\"javascript:void(0)\" onclick=\"open_page('ajaxinfo.php','content'); javascript:change('{$t}');\">{$t}</a> ";
You have to have code to add the contents returned by the ajax to the page. I don't see that anywhere.

Categories