Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my code i would like to define a function / variable / ... .
It should look like something like this:
<?php
//define something here
{
"html code inside here"
}
// some php code
if(...)
{
"output the html
}
How can I do this?
You would need to put it in a variable/function first, or echo out the html below.
Option #1:
$html = "This is test html <a href='http://google.com'>google</a>";
if ( !empty($_POST) )
{
echo $html;
}
Option #2:
if ( !empty($_POST) )
{
echo "This is test html <a href='http://google.com'>google</a>";
//Or simply:
?>
This is test html google
<?php
}
Option #3:
function outputHtml() {
?>
This is test html google
<?php
}
if ( !empty($_POST) )
{
outputHtml();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
$element = " <div class='product-info'>
<p>$Name</p>
<?php if () {}?>
<p>$Title</p>
</div>";
this variable $element contains HTML code and I want to run this if statement inside of it but without splitting the variable because I want to echo this whole variable somewhere else.
You can't do what you're asking, but you can do something that has the same effect:
$element = " <div class='product-info'><p>$Name</p>";
if (some condition) {
$element .= "some text";
} Else {
$element .= "Some other text";
}
$element .= "<p>$Title</p> </div>";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was trying to get variable value from a page and echo it out in another page
for example I have 2 pages pg1.php and pg2.php:
On pg2.php I have:
<?php
$vr = "Hello";
?>
Now I want to echo this out on pg1.php, I have tried this:
<?php
require "pg2.php";
echo $vr;
?>
It works, but the problem is whatever else I have on pg2.php will be displayed on pg1.php.
It would be best to restructure your code so that variables are defined in one file that then includes another file with output etc.
<?php
// vars.php
$vr = "Hello";
?>
<?php
// pg1.php
require "vars.php";
echo $vr;
?>
<?php
// pg2.php
require "vars.php";
// other stuff
// ...
// ...
?>
But for this issue in general, buffer output and then delete the buffer:
<?php
// pg1.php
ob_start();
require "pg2.php";
ob_end_clean();
echo $vr;
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a website in which I want to do inline styling inside php tags.
<?php
if($data['item']->hello_world!=null)
{
echo "policy:";
echo strtolower($data['item']->hello_world->name);
echo "<br>";
echo strtolower($data['item']->hello_world->description);
}
?><?php
if($data['item']->hello_world!=null)
{
echo "policy:";
echo strtolower($data['item']->hello_world->name);
echo "<br>";
echo "<br>";
echo strtolower($data['item']->hello_world->description);
}
?>
The data which I want to style coming from the php code is:
echo strtolower($data['item']->hello_world->name);
Problem Statement:
I am wondering what changes I should make in the php code so that I am able to do styling for the above mentioned line.
To inline style the element:
echo '<span style="color: red">' . strtolower($data['item']->hello_world->name) . '</span>';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to call variable with in variable and below attempts are working fine
Attempt 01:
<?php
$var1 = "Hello";
echo $var1;
?>
Attempt 02:
<?php
$view = '$var1 = "Hello";
echo $var1;';
echo $view;
?>
But I'm trying to call variable without using "echo" or "print" command, as mentioned below
<?php
$view = '$var1 = "Hello";
echo $var1;';
$view;
?>
Is there any other way to achieve this? Please advise.
<?php
$view = '$var1 = "Hello";
echo $var1;';
eval($view);
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display a text with a specific tag in wordpress.
Ex: I have 2 tags: car and boat, and 2 text: "I have car and i want one bike", "I have boat and i am happy"
I want to display in header specific text when exist tag.
I try using tem exist in header.php but display text in all pages, not just in page where exist tag.
Ex:
<?php
$boat = term_exists('boat', 'post_tag');
if ($boat !== 0 && $boat !== null) {
echo "I have boat and i am happy!";
}
?>
If you have only two tags this will do.
<?php
if (is_tag( 'boat' )) {
echo "I have boat and i am happy!";
} elseif (is_tag('car')) {
echo "I have a car";
}
?>