I am making static php and i want somthing like this (i am a php beginner :):
From this:
<?php $titleid="example title"; ?>
To this:
<?php $title="<?php echo $titleid; ?>"; ?>
To get this:
<h1><?php echo $title; ?></h1>
And then, the expected output is:
<h1>example title</h1>
I use this cause the variable $titleid is in other php file.
In this line
<?php $title="<?php echo $titleid; ?>"; ?>
You should not use the echo, since at that time you don't want any output. Also avoid the double <?php ?> brackets.
Just write that line as
<?php $title=$titleid; ?>
First, you open <?php tag only once in file. if you want get example title in your pages, you define this content one variable and then get result with echo :
Ex
<?php
$title = "<h1>example title</h1>";
echo $title;
Related
So I have header.php file with my header and page1.php, page2.php, page3.php with my content. Every page starts from
<?php include_once("header.php") ?>
But title is always static in my code. I tried to add <title><?php $title_value ?></title> to my header.php, and
<?php $title="Contacts"; include_once("header.php") ?>
to my page1.php but title is invisible. It seems like $title didn't exist. How can I fix it and add different titles to my pages?
<?php $title_value ?> does nothing. You need to echo/print the value in the header.php.
<title><?php echo $title_value; ?></title>
In header.php you also should include a check incase you forget a title in a file.
$title_value = empty($title_value) ? 'Default Title' : $title_value;
You only declared the variable.
You didnt print or echo it.
Use:
<?= $title; ?>
Or:
<?php echo $title; ?>
Or:
<?php print $title; ?>
first you have to echo the value of the title like that:
<?php echo($my_var) ;?>
// or
<?=$my_var?>
after to change the title of your page u can pass a $_GET param to your html title value ( not the better way but the easier )
ex:
// on the fist page
<a href="/myurl?title=page2">
// on the second page
<?php echo($_GET['title']) ;?>
The below code will not display any output as the variable is declared below the echo as PHP gets executed line by line. Is there any way to search for the variable in the whole page and then execute the code?
<?php
include "header.php";
$title = "Test";
?>
header.php
<html>
<head>
<title><? echo $title ?></title>
</head>
You need to learn how compilers/interpreters works. PHP is interpreted language and The binary that lets you interpret PHP is compiled.
PHP run from top to bottom.
so its like
<?php // start from here
echo "$title"; <-- $title is undefined here
$title = "Test"; <-- now you declared $title with value so it goes in memory now
//end
So you need to first check weather $title is set or not than respond according to it
if(isset($title)){
echo $title;
}
According to your logic, I suggest you to use contants like below:
Create a separate file, let's say constant.php and include it on all other pages
<?
define("TITLE", "This is title");
?>
Use it like below:
<?php echo TITLE;?>
Thanks
I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.
In my case, I'd like to use code ore like this:
$somevar = 'a test';
include("file.html");
and file.html would contain
<b>hello, this is {$somevar}</b>
The problem is that it just prints hello, this is {$somevar}.
How can I make the HTML read the vars in the included file?
echo "<b>hello, this is {$somevar}</b>";
or
<b>hello, this is <?=$somevar?></b>
or
<b>hello, this is <?php echo $somevar; ?></b>
or
<b>hello, this is <?php print $somevar; ?></b>
You need to include the variable defining program, in the other program wanting to access it.
Example:
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>
I am setting the value of a php variable to some html. i.e.
$_img = 'hehehehehe';
The variable is then shown in html after a br tag. But it doesn't execute the html in it. Rather it displays it like hehehehehe. So, is there any problem in my code! How can i do this thing?
Here is the code that displays that IN HTML,
<?php if ($_item->getComment()): ?> <br/><?php echo $this->escapeHtml($_item->getComment(), array('b','br','strong','i','u')) ?> <?php endif; ?>
<?php
$string = 'Hehehe';
echo $string;
?>
This works fine! The html is 'executed' and the link is displayed.
From your comment....
Here is the code that displays that <?php if ($_item->getComment()):
?> <br/><?php echo $this->escapeHtml($_item->getComment(),
array('b','br','strong','i','u')) ?> <?php endif; ?>
As predicted by many people, it looks like you are encoding the value when you display it.
I don't know what the $this->escapeHtml function is doing exactly, but it would appear to be doing an HTML Encoding on the string.
The result being that any tag, for example <a> will be sent to the browser as <a> which the browser will display as <a>. The browser will not see it as a tag, and will therefore not treat it as one.
So the simple answer is: don't encode the HTML...
<?php echo $_item->getComment(); ?>
I suspect you are just echoing the variable.
You need use the 'htmlspecialchars' method such as below.
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
I have the 2 following echos :
<?php echo $data->county; ?>
This one gives me datas like "florida", "california"... from the database
and
<?php echo lang(california); ?>
This one gives me a translation from a lang.php file, for example :
'california' => 'La californie'
I would like to place the $data->county in the lang echo, I tried the following with no success :
<?php echo lang(.$data->county.); ?>
What's the error ? Is it possible to echo in an echo ?
What made you think you needed the dots? Just pass it like any other argument:
<?php echo lang($data->county); ?>
<?php echo lang($data->county); ?>
Lose the .s, they're for concatenating strings. You're just passing a string variable.