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);
?>
Related
I have a code that can get me the category_id of an item. This is the code:
<?php echo lavada_category_id() ; ?>
I want to know how I can add this code. Inside this, I want to replace the number 2 in here;
<?php lavada_query_item("category=2");?>
with:
<?php echo lavada_category_id() ; ?>
I know you cannot do like this
<?php lavada_query_item("category=<?php echo lavada_category_id() ; ?>");?>
But how can I do it?
Why not store it into a variable and then use that variable?
<?php
$catID = lavada_category_id();
lavada_query_item("category={$catID}");
?>
OR if you just want category ID to be passed into lavada_query_item do this:
lavada_query_item($catID);
The syntax error that you have is that you can not use <?php within <?php
You just need to concatenate the string like this:
<?php lavada_query_item("category=". lavada_category_id() );?>
I think this is what you are looking for:
<?php lavada_query_item(lavada_category_id());?>
The value returned from thelavada_category_id() function will be passed into the lavada_query_item() function.
For example, I have a page called 'include.php' or 'include.html', which contains the following lines of code:
<h1>Hi {$yourname}, this is a header.</h1>
Now, I want to include this page using PHP on another page, let's say on 'index.php' using:
<?
$yourname = 'Bir';
include 'include.php';
?>
When I include it, the page shows:
<h1>Hi {$yourname}, this is a header.</h1>
The variable $yourname is not replaced by it's value "Bir".
How do I solve this problem?
Need to write in include.php as
<h1>Hi <?php echo $yourname ;?>, this is a header.</h1>
Another simple way..
test1.php
<?php
$var="<h1>Hi $yourname, this is a header.</h1>"; //Removed {}
test2.php
<?php
$yourname="Jacob";
include_once('test1.php');
echo $var; //"prints" <h1>Hi Jacob, this is a header.</h1>
Check out http://us3.php.net/str_replace, and replace wherever you see {$yourname} with what ever the value really is.
if you want to avoid <?php ?> then you have to use code like this
<?php echo "<h1>Hi ".$yourname." this is a header.</h1>";
use this
<h1>Hi <?= $yourname ?>, this is a header.</h1>
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')]'); ?>
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\"]") ?>
I'm using WordPress. I appreciate being shown the code, but this is one I am interested in straight out learning how to do myself too - so if you know where I can find a tutorial or can give me information I'd appreciate it!
I'm calling posts and want to include a PHP code within a PHP code, this is for a theme options panel.
<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged);
if (have_posts()) :
?>
Where the 51 is I want to put:
<?php echo get_option('to_postc_home'); ?>
Where the 3 is I want to put:
<?php echo get_option('to_posti_home'); ?>
If I'm interpreting right, this is what you need, use the concatenation operator . to use those functions in place of plain text ex: 'this is text' versus 'this '.get_option('stuff').' text'
<?php
query_posts('cat='.get_option('to_postc_home').'&posts_per_page='.get_option('to_posti_home').'&paged='.$paged);
if (have_posts()) :
?>
To include a php file from another file you use the include function
You can use it whereever you want
<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged);
if (have_posts()) :
?>
hello world
<?php echo get_option('to_postc_home');
endif;
<?php
$a = get_option('to_postc_home');
$b = get_option('to_posti_home');
query_posts("cat={$a}&posts_per_page={$b}&paged={$paged}");
if (have_posts())
?>
That is called string concatenation, and you are already using that on the first line of your code, when you concatenate the literal string 'cat=-51&posts_per_page=3&paged=' with the variable $paged. In PHP, the . operator does that.
So, in your code, you can do this:
<?php
query_posts('cat=-' . get_option('to_postc_home') . '&posts_per_page=' . get_option('to_posti_home') . '&paged='.$paged);
?>
This will inject the output of the function calls at the places you indicated.
<?php
$cat = get_option('to_postc_home');
$per_page = get_option('to_posti_home');
query_posts("cat=${cat}&posts_per_page=${per_page}&paged=".$paged);
if (have_posts())
?>