HTML- and PHP-code inside an PHP 'if statement' - php

What if I want to have HTML code inside my 'if' statement, and then php code in the 'else' part? What do I do? I have tried this, but with no luck..
<?php
session_start();
if ($_SESSION['bnavn']) : ?>
'HTML-code'
<?php else : ?>
'PHP-code'
<?php endif: ?>
I have also tried this, but also with no luck:
<?php
session_start();
if ($_SESSION['bnavn']) : ?>
'HTML-code'
<?php else : ?>
<?php 'PHP-code' ?>
<?php endif: ?>

I have figured it out! I found this link: PHP Session Destroy on Log Out Button
I made the if statement as one of the answers said, and changed my page.html to page.php. I do not know if it have to .php instead of .html, but not it works!

Related

Why does my included/required file with variables stop working when I upload everything?

My index.php gets all parts for the website seperated and it looks like this:
<?php
include 'var1.php';
?>
<?php
include 'var2.php';
?>
<?php
include 'https://www.example.de/city/head.php';
?>
<?php
include 'https://www.example.de/city/header.php';
?>
<?php
include 'https://www.example.de/city/content.php';
?>
<?php
include 'https://www.example.de/city/footer.php';
?>
<?php
include 'https://www.example.de/city/bottom.php';
?>
The main parts like head, header etc. are always the same but for each city I need different variables and thatswhy I got a folder (for example 'Berlin') with only index.php, var1.php and var2.php in it.
This is working fine on my computer but as soon as I upload it to get it online, the variables from var1.php and var2.php are not included anymore.
Can anyone see something wrong here?
Fixed it with:
include($_SERVER['DOCUMENT_ROOT'].'/city/head.php');

Add Google Plus in header in html.tpl.php

In html.tpl.php I have written :
<?php if($front_page):?>
<?php print 'Google+ '?>
<?php endif; ?>
but its not working moreover its hanging the home page.
Can someone please tell me the correct syntax if it has some error
Should you not be using echo instead of print?
<?php if(drupal_is_front_page()):
echo 'Google+';
endif; ?>
In most themes, you should be overriding page.tpl.php rather than html.tpl.php. You also have a couple of errors in your syntax. The typical code for a template would read:
<?php if($front_page): ?>
Google+
<?php endif; ?>
Note the space in the first line, plus you don't need to print the second.

php include breaking

I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>

Whats the difference between <?php functionhere(); ?> and <?=functionhere();?>

Can someone explain to me when <?= needs to be used or why this programmer would code this way? I'm working on creating a third party module for SPBAS and I nearly figured it out, I just don't know the significance of the two different options I've specified.
Thanks in advance.
<?= functionhere(); ?> is a short hand for <?php echo functionhere(); ?>.
what <?=something?> is the short form of doing <?php echo something; ?>
where as <? something; ?> does whatever something was supposed to do
edit: im generalizing something as any php call, function string, array, object etc..
<?php functionhere(); ?> does not print out the results from the function, <?=functionhere(); ?> does.
This is a shortcut syntax to echo the variable that comes after it. It has the same effect as
<?php echo $variable; ?>
or
<?php echo functionhere(); ?>
in your case.
<?php functionhere(); ?>
will not do anything. unless something is printed out inside the function
For this to work, short_open_tag has to be enabled
<?= functionhere(); ?> = <?php echo functionhere(); ?>
<? functionhere(); ?> = <?php functionhere(); ?>
They are called short tags and can be enabled via the PHP configuration.
They do the same thing. Only difference is <?php is proper syntax.
One is short tag for echo - but it should not be used because if this function is turned off it will output your code. Thanks for the vote down.

Reasoning for alternate PHP syntax for conditional HTML

So, why does this work:
<?php if (condition): ?>
<!--html code here-->
<?php endif; ?>
But not simply this:
<?php
if (condition) { ?>
<!--html code here-->
<?
}
I figured it was just style preference, but I actually can't seem to get it to work the second way. Am I just doing something completely wrong? I can't see the purpose of outputting HTML right in the middle of an if statement if you wanted it to always print.
You need to allow short tag in your php.ini to make <? work
else you have to write <?php } ?>
The second way should work like you described considering you have the closing php tags and < ?php
It does, if you have the right re-opening tag...
<?php
if (condition) { ?>
<!-- html code -->
<?php
}

Categories