Simple PHP question - php

Yes, it's a simple question, but one that I can't find a answer for through the PHP documentation or Google. (I'm just learning PHP....)
If this works:
<?php $d=date("D"); if ($d="Mon") { ?>echo this text on Monday<?php endwhile; ?><?php } else { ?><?php } ?>
Why doesn't this?
<?php $d=date("D"); if ($d="Mon,Tue") { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>
Do I need different delimiters between Mon and Tue? I've tried || and && ....
Thanks, Mark

You're performing an assignment of $d when you say ($d="Mon"). What you want is the comparison operator (==):
if ($d == "Mon" || $d == "Tue")

You're assuming that date("D") will return more than one value. It will only return the current day. Instead use this:
<?php $d=date("D"); if (in_array($d, array("Mon","Tue"))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

The string $d is either going to contain "Mon" or "Tue", never "Mon,Tue". You can't compare strings this way. You need to use an expression like this:
if ($d == "Mon" || $d == "Tue") {

try:
<?php $d=date("D"); if (in_array($d,array('Mon','Tue'))) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

Maybe this:
if ($d == "Mon" || $d == "Tue") {
also, php has two operators for equality.
== and ===

If you've string with value 'Mon,Tue', Then you can check
if($d=='Mon,Tue')
There is no chance for that, So you need to use OR condition.
ie,
if($d=='Mon' || $d=='Tue')

Try this code which is more user readable:
<?php $d=date("D");
$days=array("Mon,Tue");
if ($d="Mon,Tue") if(in_array($a,$days)) { ?>echo this text on Monday and Tuesday<?php endwhile; ?><?php } else { ?><?php } ?>

Related

How to add html inside an if else statement in php?

I have this code where if something then display a link, else display another link. the if and else are php code, and the links are html as you will know, but I was wondering how do you make it so it will not give me any errors, how do I combine php with html?
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
Leave Society;
} else {
Join Society;
}
This might help you. This one is from the basics of PHP:
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "<a href='file.php' class='socbuttons'>Leave Society</a>";
} else {
echo "<a href='anotherfile.php' class='socbuttons'>Join Society</a>";
}
}
?>
Remember PHP should be surrounded by <?php ?>. Simply end the "PHP part" and then start it again when you finished your HTML.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
?> Leave Society <?
} else {
?> Join Society<?
}
Also, you can write HTML in echo statement, but you should escape some special characters like " to not interfere with the php code itself.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "Leave Society";
} else {
echo "Join Society";
}
See http://php.net/manual/en/function.echo.php to more information.
Try this
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) { ?>
Leave Society;
<?php } else { ?>
Join Society;
<?php }
Use alternate syntax:
<?php
$aUserSocs = array( 'link1', 'link2', 'link3' );
$soca = 'link2';
$iCountUserSocs = count( $aUserSocs );
for( $i = 0; $i < $iCountUserSocs; ++$i ):
?>
<?php if( $soca == $aUserSocs[ $i ] ): ?>
Leave Society;
<?php else: ?>
Join Society;
<?php endif; ?>
<?php endfor; ?>

Display html if one of more variables are not empty

I want to echo some html if a variable isn't empy, for this I know I can do the following:
if (!empty($test)) {
?>
<p>Some nice html can go here</p>
<?php
}
else
{
echo "It's empty...";
}
How can I do this for several variables? So if one of the variables are not empty then echo the html? Would this do it?
if (!empty($test || $image || $anothervar)) {
?>
<p>Some nice html can go here</p>
<?php
}
else
{
echo "It's empty...";
}
Just try with:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
// ...
}
You should check every variable:
!empty($test) || !empty($image) || !empty($anothervar)
empty function does not take multiple arguments.
So, you need to user empty separately for each variable.
The final code should be:
if (!empty($test) || !empty($image) || !empty($anothervar)) {
Just check all the three variables.
Also, I advise you to embed your php into your html to have a better readable document, like this:
<?php if (!empty($test) || !empty($image) || !empty($anothervar)): ?>
<p>Some nice html can go here</p>
<?php else: ?>
It's empty...
<?php endif; ?>
Just another solution:
if(empty($test) and empty($image) and empty($anothervar)) {
echo "It's completely empty...";
} else {
?>
<p>Some nice html can go here</p>
<?php
}
Or if you have a lot of variables to check:
$check = array("test","image","anothervar");
$empty = true;
foreach($check as $var) {
if(! empty($$var)) {
$empty = false;
}
}
if($empty) {
echo "It's completely empty...";
} else {
?>
<p>Some nice html can go here</p>
<?php
}

Can i use HTML instead of echo in IF-ELSE statement in PHP?

if( $user->username == 'XYZ' )
{
echo "hello, XYZ";
}
else
{
echo "hello, guest";
}
In the above code, can i use pure html code which will get executed incase the IF statement is true instead of using echo ?
Yes, you can do it:
if( $user->username == 'XYZ' )
{
?>
hello, XYZ
<?
}
else
{
?>
hello, guest
<?
}
Sometimes it looks better and simplier. But in fact it is much better to put php and html code in different files (separate logic, styles and data).
if( $user->username == 'XYZ' )
{ ?>
<p>Hello <b><i>XYZ</i></b>
<?php }
else { ?>
<p>Hello <b><i>Guest</i></b>
<?php
}
I hope You Got it.
Of course you can write HTML directly by closing the <?php ?> tags.
<?php if( $user->username == 'XYZ') { ?>
hello, XYZ<br>
<?php } ?>
<?php else { ?>
hello, guest<br>
<?php } ?>
You can of course heredoc as well:
<?php
$str = <<<FOO
This is a
demo message
FOO;
echo $str;
?>
but i dont use it since i it messes with any highlighting editor (it thinks it's a text and i like my HTML highlighted)
What i like best is this, especially when my outputs are big:
<?php if( $user->username == 'XYZ') {
include("user_template.php");
}
<?php else { ?>
include("guest_template.php");
<?php } ?>
which are actually just rendering HTML contained there.
Can also be written like this, more readable for several lines.
<?php if ($user->username == 'XYZ') : ?>
Hello, XYZ
<?php else : ?>
Hello, Guest
<?php endif; ?>

How do I close this php statement correctly?

Im attempting to create a simple list out of some elements I stole from a more complex list of rows etc., I just need list out the values in single row separated by commas.
<?php foreach ($document_items as $document_item)
{
if ($document_item->document_id == $document->id)
{
if (nbf_common::nb_strlen($document_item->product_code) > 0)
{
echo nbf_common::nb_strlen($document_item->product_code);
}
;} ?>
;} ?>
<?php } ?>
The Result I get follows "3 ;} ?> 3 ;} ?> 4 ;} ?> 3 ;} ?> "
Thanks in advance
Micah
try this
<?php
foreach ($document_items as $document_item)
{
if ($document_item->document_id == $document->id)
{
if (nbf_common::nb_strlen($document_item->product_code) > 0)
{
echo nbf_common::nb_strlen($document_item->product_code);
}
}
}
?>
Fore more detail PHP Tags
Change the #9, #10, remove #11 line. What you're doing is printing characters: ;} ?>
and are syntactically wrong. This is proper:
<?php foreach ($document_items as $document_item)
{
if ($document_item->document_id == $document->id)
{
if (nbf_common::nb_strlen($document_item->product_code) > 0)
{
echo nbf_common::nb_strlen($document_item->product_code);
}
} // here
} // here
?>
Also for the "comma separated" part, put required values in a variable and echo it at the end.
Could be like this:
<?php
$string = '';
foreach ($document_items as $document_item)
{
if ($document_item->document_id == $document->id)
{
if (nbf_common::nb_strlen($document_item->product_code) > 0)
{
$string .= nbf_common::nb_strlen($document_item->product_code).',';
}
}
}
echo rtrim($string, ','); // remove the last comma
?>
or use a temp array to glue them at the end:
<?php
$lines = array();
foreach ($document_items as $document_item)
{
if ($document_item->document_id == $document->id)
{
if (nbf_common::nb_strlen($document_item->product_code) > 0)
{
$lines[] = nbf_common::nb_strlen($document_item->product_code);
}
}
}
echo implode(',', $lines); // bind them with comma
?>
I'm not sure why you have all those extra ?>. The pattern is:
<?php
// PHP code goes here
?>
i.e. every <?php has a matching ?>; no more, no less.1
1. Other than the case that #Mihai points out in the comment below...
To understand the php tags you might want to think that you are in an HTML file and you are intrerrupting the HTML flow with
When writing PHP only files (classes, interfaces, traits lists of functions and similar grouping of application code with no templating) it is advisable to open at the very first character in the file and not end it at all.
When writing template files however it is advisable to use php's alternate syntax:
<?php if($x): ?>
<?php elseif($y): ?>
<?php else: ?>
<?php endif; ?>
Instead of the standard:
<?php if($x) { ?>
<?php } else if($y) { ?>
<?php } else { ?>
<?php } ?>

Puzzling around with Get Page ID

Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...
<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>
<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.
$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)

Categories