php within a php statement - php

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]");
}
?>

Related

Getting an error about unclosed opening bracket for a function in PHP

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() ?>

Using html code in php function or no?

i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>

$string is not showing in common.php

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!

output 2 string in one function

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.

php if mobile_device_detect is true conditional statement

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

Categories