I don't understand PHP very well... not good start I know.
I'm using http://detectmobilebrowsers.mobi/ for mobile/desktop website, and I'm using wordpress as my CMS.
I have set up my function like so...
require_once('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,true,true,false,false,false);
But now in my theme, I want a conditional statement similar to this...
<?php if (is_home()) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Though I want it to return the values from my function. See below my rubbish attempt below...
<?php if (mobile_device_detect==true) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Obviously this is not going to work lol, but can you see what I'm trying to achieve? and help would be most awesome thanks!
Cheers
Josh
Nearly there.
require_once('php/mobile_device_detect.php');
$mobile = mobile_device_detect(true,true,true,true,true,true,false,false,false);
Then check for
if ($mobile==true) // then do the rest of your stuff
Related
I have this code:
<?php
function good_day() { ?>
'Good Day';
<? }
good_day();
?>
It gives me an error about unclosed { which I expect. However, It is inspired from the code ina a blog post that I was reading. It looks like this:
function social_media_links() { ?>
<ol>
<li>Facebook</li>
.....
</ol>
<?php }
add_filter( 'footer_links', 'social_media_links' );
Is this code valid or invalid in the context of WordPress? Is there any file in WordPress where I could put the above code and it would be considered valid?
Lets say the code is valid and it will work in WordPress, isn't using echo still a better approach for doing this?
You should declare it like this:
<?php
function good_day() {
echo 'Good Day';
}
?>
And use it like this:
<?php good_day() ?>
when i use first code it works but not working for second one.
first code that works fine
<?php
session_start();
if(!isset($_SESSION['user_email'])){
//if not logged in
header ("location: login.php");
}
else {
?>
welcome page
<?php } >
But when i try to do this only "if" statement is working "else" is showing nothing
<?PHP
session_start();
if(!isset($_SESSION['user_email'])){
?>
html codes here (works fine )
<?php}
else { ?>
diffrent html codes here
(but this section is not working)
<?php }?>
what is wrong and how should i close "{} " in this case ...please help
Hi Jannatul!
In your second PHP opening tag, you have this:
<?php}
This without a space may be causing issues while parsing your code. Try and replacing it to this:
<?php }
If this works please tell me so, and if it doesn't I will gladly be willing to assist you further.
Matt
Try this without that mess..
<?php
session_start();
if(!isset($_SESSION['user_email']))
{
echo 'html codes here (works fine )';
}
else
{
echo 'diffrent html codes here (but this section is not working) - but maybe now does';
}
?>
Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!
Ok, I must admit, I am a rather PHP noob who struggles with what seems to me a simple issue.
What i want to accomplish is that I can put 2 strings in 1 function and echo them in different places.
In common.php i have this:
<?
function printHeader($titel) {
?>
Later on in the file i echo it between
And in index.php i have this:
<?
include "common.php";
printHeader('this is my title');
?>
This works alright.... now what i like to do is to ad another String to the printheader to not only echo the title but also the H1, so i tried this:
Common:
<?
function printHeader($titel . $headtitle) {
?>
Index:
<?
include "common.php";
printHeader('This is my title!' . 'This is my H1');
?>
This does not seem to work. Are there any phpsavvy guys out here who can help me wit this simple problem?
If it is not to much to ask I would also like to Echo some standard value if $headtitle is empty, but that is waaaay of my league :)
EDIT: thanks to you guys the first problem is fixed. Now i want to try and fix the IF empty part. So I came up with this:
<?
function printHeader($titel, $headtitle) {
if (empty($headtitle)) {
echo 'title is empty'; }
?>
html goes here + this: <h1><?=$headtitle; ?></h1> more HTML
<? } ?>
This does not seem to work...
any help would be appreciated!
Thanks in advance,
A simple webdesigner ;)
Function arguments should seperate by a comma ,
function printHeader($titel , $headtitle)
And obviously same for calling,
printHeader('This is my title!' , 'This is my H1');
You have to seperate the second the argument by , like this
function printHeader($titel , $headtitle)
and change while calling also
printHeader("string1","String2");
or keep like this
function printHeader($titel){..... }
printHeader("string1"."String2");
so $title1 will have appended string.
Using Wordpress, i have a plugin that inserts a playable MP3 on the page.
To call that, along with the track details, this code is inserted;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:http://thewebsite.com/thetrack.mp3|artists=Artist|titles=Titles]");
} ?>
I would like to make this editable from the backend easily, by entering some meta-data. So this;
<?php meta('track-url'); ?>
Along with other various details would replace those that are above.
Unfortunately for me, this;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:<?php meta('track-url'); ?>|artists=Jack Presto|titles=Track 1]");
} ?>
obviously does not work! This is down to my lack of understanding if PHP - can anyone help?
Cheers!
Simple! Do this:
<?php if (function_exists("insert_audio_player")) {
$trackUrl = meta('track-url');
insert_audio_player("[audio:$trackUrl|artists=Jack Presto|titles=Track 1]");
} ?>
I can't tell if the meta() function prints to the screen or returns a string. If it returns a string, do:
<?php
if (function_exists("insert_audio_player")) {
insert_audio_player('[audio:' . meta('track-url') . '|artists=Jack Presto|titles=Track 1]');
}
?>
If it prints to the screen, it's a bit more complicated. Ideally, you would have a function that DOES return a string, but as a quick hack (if you're only getting paid for a quick fix) you could do something like
<?php
if (function_exists("insert_audio_player")) {
ob_start();
meta('track-url');
$meta = ob_get_contents();
ob_end_clean();
insert_audio_player("[audio:$meta|artists=Jack Presto|titles=Track 1]");
}
?>