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.
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() ?>
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!
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
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]");
}
?>
In a little trouble with this, is they any way i can get to echo's working together see code below,
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed(' <?php echo $youtube_vimeo_player['url']; ?> ', '', ''); ?>
I'm wanting the info brought from the vimeo_player url to be entered into the video->embed section.
Any help on this would much be appreciated : )
try it like this:
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed( $youtube_vimeo_player['url'], '', ''); ?>
Replace
' <?php echo $youtube_vimeo_player['url']; ?> '
with
"{$youtube_vimeo_player['url']}"
You dont need echoing inside the php string. Note that { and } are the special way to embed array index, or object method call into the string, they are not present in final string.
Btw, it's sufficient to just do
echo $video->embed($youtube_vimeo_player['url'], '', '');
As $youtube_vimeo_player['url'] already shoud be the string
<?php echo $video->embed("'".$youtube_vimeo_player['url']."'", '', ''); ?>
PHP isn't that crazy, but thanks for remembering us it could seem to.
Thou shall write:
<?php
$youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE);
$url=$youtube_vimeo_player['url'];
$video->embed($url, '', '');
?>
Educate yourself on the concept of variable, and remember, php has only one level of embedding (that is one level of <?php ... ?> — no nesting)
And believe it, it's better this way.