Can I insert php into parameters? - php

Sorry if I worded the question wrong. In the code below, is there a way to insert php where the date is?
So this:
<?php echo do_shortcode('[fergcorp_cdt_single date="08 December 2013"]') ?>
Would become something like this:
<?php echo do_shortcode('[fergcorp_cdt_single date="<?=$date?>"]') ?>
Is the above correct? If not, how would I write it?

<?php echo do_shortcode('[fergcorp_cdt_single date="' . $date . '"]'); ?>

You could also use double quotes:
<?php echo do_shortcode("[fergcorp_cdt_single date=\"$date\"]") ?>

Related

general php syntax

how do i put this <?php echo $cfs->get('exclude'); ?> within the double quotes in this <?php echo do_shortcode('[gallery exclude=""]'); ?>
meaning, what is the correct syntax to merge the two? i attempted to read through some of this to solve it, but have little php knowledge.
thank you.
sprintf('[gallery exclude="%s"]', $cfs->get('exclude'));
See sprintf()
Also readable:
$exclude = $cfs->get('exclude');
"[gallery exclude=\"$exclude\"]";
this is one way
echo do_shortcode('[gallery exclude="' . $cfs->get('exclude') . '"]'); ?>
<?php echo do_shortcode("[gallery exclude='$cfs->get('exclude')]'); ?>

How to include php variable url?

Currently, as an example, I have this php code on one of my pages.
<?php include("codes/games/scripts/titles/angrybirds.php"); ?>
How would I change this to make my variable:
$gametitle = "angrybirds";
work like this:
<?php include("codes/games/scripts/titles/$gametitle.php"); ?>
Thanks in advance for your help!
That code should work as-is, since the . won't be interpreted as part of the variable name. You can see the output of it here: http://codepad.org/ZbtiOPgB
However, for readability, I would encourage you to either use clear concatenation:
<?php include("codes/games/scripts/titles/".$gametitle.".php"); ?>
Or wrap your variable name(s) in { and }:
<?php include("codes/games/scripts/titles/{$gametitle}.php"); ?>
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
Or:
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
<?php
$file = 'codes/games/scripts/titles/' . $gametitle . '.php';
if (file_exists($file))
require_once($file);
?>

Is this possible to use a variable for an url in php?

I'm calling a function like this:
<?php print get_thumbnail('http://url.com/?skin=rss'); ?>
Being a php newbie, I'm wondering if there is a way to change the http://url.com part based on a custom metadata I have set up in Wordpress. So I guess it would look something like this:
<?php print get_thumbnail('<?=$video_src?>/?skin=rss'); ?>
Is something like this possible?
Yes, you have the right idea, you just don't need to re-open PHP tags since you're already inside some. You can use . to concatenate (join together) the value of $video_src and "?skin=rss".
<?php print get_thumbnail($video_src . "?skin=rss"); ?>
Try this:
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>
Keep in mind that <?= $foo ?> is shorthand for <?php echo $foo; ?>. <?= ?> won't be expanded in strings, but you can achieve something similar using double quoted strings:
<?php print get_thumbnail("$video_src/?skin=rss"); ?>
Yes, except within PHP, you don't need to enter the PHP tags again.
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>

Make HTML tag in PHP

I have some php code $_SESSION['username'] and I need to display it using html with something like <p>the username here</p>. I heard you might be able to do this using echo in PHP but Im not sure how. Thanks for any help.
echo '<p>' . $_SESSION['username'] . '</p>';
or
<p><?php echo $_SESSION['username'] ?></p>
You should really run it through htmlspecialchars to avoid breaking your HTML syntax (for example if the user's name contains </p> or something, or if they attempt an XSS attack):
echo '<p>' . htmlspecialchars($_SESSION['username']) . '</p>';
If I understood you correctly, I believe you are looking for:
<p><?php echo $_SESSION['username']?></p>
<?php echo '<p>'.$_SESSION['username'].'</p>'; ?>
Check out the documentation:
http://www.php.net/manual/en/tutorial.useful.php

Where is the problem in this PHP code?

There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.

Categories