I'm using this php mobile detect script https://github.com/serbanghita/Mobile-Detect
And I have singled out this code that I want to use, however I want to throw in an 'else' option, but I seem unable to do so.
This is the code
<?php $check = $detect->isMobile(); if($check): ?>
Hello you're on a phone
<?php endif; ?>
This is what I want to achieve
<?php $check = $detect->isMobile(); if($check): ?>
You are phone
<? else ?>
You're something else
<?php endif; ?>
But when I do it throws up a Parse error: syntax error
You can try this simpler syntax:
<?php $check = $detect->isMobile(); if($check){ ?>
You are phone
<? } else { ?>
You're something else
<?php } ?>
The above is better to understand, but you can use this too:
<?php $check = $detect->isMobile(); if($check): ?>
You are phone
<? else: ?>
You're something else
<?php endif; ?>
I would rather say, use the first part . Though the second syntax is good, but the first is easier to understand.
You forgot : after else on 3rd line,
<?php $check = $detect->isMobile(); if($check): ?>
You are phone
<? else: ?> // here
You're something else
<?php endif; ?>
Related
I'm trying to use the php get. everthing is working find exept the include page.
it's adding 1 at the end of include content
any ide how do i ride that?
example:
I love norway
I love norway1
Code:
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') $norge = include('norway.php');
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
We should use file_get_contents, instead of include.
$norge = file_get_contents("norway.php");
Use the output buffer functions to capture the output from the included script.
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') {
ob_start();
include("norway.php");
$norge = ob_get_clean();
}
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
is it possible to have different headers on each page?
I'm currently using this code which works perfectly
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
<?php endif;?>
Now I tried to use this code and it doesnt work
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
else { ?> (is_page('Contact')){
echo '<img src="image5.jpg" />';
}
<?php endif;?>
Any ideas?
I know nothing about Wordpress - never used it but the above doesn't look right. Is there a reason whya more traditional style syntax is not adopted - more akin to this:
<?php
if( is_front_page() ){
echo do_shortcode('[myiamge1]');
} elseif( is_page('Contact') ){
echo '<img src="image5.jpg" />';
} else {
/*banana for scale - do something else*/
}
?>
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; ?>
Trying to exclude a piece of code from running on certain pages. I would like to do something like the following, but this syntax isn't correct.
<?php
if (!is_page('Blog') || $post->post_parent=="19")) {
<?php breadcrumbs(); ?>
} else {}
?>
First error is - PHP tag was not closed.
And second is - there should be opening of PHP tag before else condition.
try this
<?php if (!is_page('Blog') || $post->post_parent=="19")) { ?>
<?php breadcrumbs(); ?>
<?php } else {
//
}
?>
I have a foreach loop inside of an if statement. My code is in shorthand format. I do not want the foreach loop to execute if the if condition returns true and instead echo out a message 'sorry database is empty!'
Functional Code
<?php if (!$check == 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php endif; ?>
<?php if ($check == 0) echo 'Sorry database is empty!'; ?>
What I don't like about this code is that I have two separate if conditions. Granted it works as it should but I would prefer to to have an else in the first if.
What is the proper syntax for what I want to do?
Alternatively if your answer is something like this:
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Explain how that would stop the foreach from executing.
Just use an else:
<?php if ($check !== 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php else: echo 'Sorry database is empty!'; ?>
<?php endif; ?>
You can also shorten this:
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
to just this:
$var_is_greater_than_two = ($var > 2); // returns true
Why not just add an else to the if ? The code block looks like special rendering, similar to Zenf Framework view script. Unless the special syntax disallows an else, you should be able to do this way.
<?php if (!$check == 0) : ?>
<?php foreach... ?>
...
<?php endforeach; ?>
<?php else : ?>
Sorry database is empty!
<?php endif; ?>
And idea could be to extract the foreach loop to another method.
Another file or same file have your executeLoop method:
<?php
function executeLoop()
{
foreach(..)
{
//do something
}
}
?>
enter code here
A few lines of code later, you can :
<?php (condition)?executeLoop() : echo $something;?>
The good thing is you can reuse your code if you wish later.