What is the right way to put HTML in PHP [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm programming in PHP, but I don't think its the right way.
I'm programming something like this:
<?php
$apple = 10
if(apple >= 4){
?>
<img src="Bladieblabla">
Biep
<?php
}else{
print "Awhh we don't have 4 apples";
}
?>

You can use echo, or print like this
if($apple > 5) {
echo '<img src="Bladieblabla">';
echo 'Biep';
} else {
print "Awhh we don't have 4 apples";
}
Update: Using echo only one time:
echo '<img src="Bladieblabla">
Biep';

<?php
$apple = 10;
if(apple > 5){
echo '<img src="Bladieblabla">';
echo 'Biep';
}else{
print "Awhh we don't have 4 apples";
}
?>

Related

How to do inline styling in html/php? [closed]

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>';

Call variable without echo or print [closed]

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);
?>

php - How do I add div in a php function? [closed]

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 7 years ago.
Improve this question
I am trying to add a <div> to my php, but everything I've found on google hasn't worked for me. I don't know what I'm doing wrong. Any ideas on how to add a <div> in a php function for styling purposes only?
<?php
//get_additional is a custom function
ob_start();
echo get_additional('Cosmos','Trades');
echo nl2br("\n");
//I want the div to start here
$buffered = ob_get_clean();
echo $buffered;
$times = $buffered;
$imgurl = 'Images/cat.png';
for($i=0;$i<$times;$i++){
echo '<img src="'.$imgurl.'" />';
}
?>
You need to echo it.
Like you've already used once in your code, echo '<img src="'.$imgurl.'" />';
Now coming back to your code:
<?php
//get_additional is a custom function
ob_start();
echo get_additional('Cosmos','Trades');
echo nl2br("\n");
//I want the div to start here
echo '<div class = "blah">
// whatever you want to do here
</div>';
$buffered = ob_get_clean();
echo $buffered;
$imgurl = 'Images/cat.png';
for($i=0;$i<$times;$i++){
echo '<img src="'.$imgurl.'" />';
}
?>

PHP echo statement IF parse error [closed]

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 4 years ago.
Improve this question
if ((isset($_GET['Menu']) && $_GET['Menu'] == "1" && $_GET['Overview']==1)
{
echo '$_GET['Overview']'
}
if statement to receive parameter from the URL
Because you cannot use if the way you do. Split up the echo, then it should work:
echo "<li><a " ;
if($_GET['Menu']==1) { echo 'class="select"'; }
echo " href=javascript:setParam('Menu',1);>Networks</a></li>";
$x="";
if($_GET['Menu']==1)
$x= 'class="select"';
echo "<li><a>".$x." href=javascript:setParam('Menu',1);>Networks</a></li>";
Im partial to sprintf for concatenation, but thats just me:
echo sprintf('<li><a %s href=javascript:setParam(\'Menu\',1);>Networks</a></li>',
$_GET['Menu']==1 ? 'class="select"' : '');
You need to echo every part of your code:
echo "<li><a ";
if($_GET['Menu']==1) { echo 'class="select"'; }
echo " href=javascript:setParam('Menu',1);>Networks</a></li>";

PHP json_parse trakt API [closed]

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 9 years ago.
Improve this question
<?php
$response = file_get_contents("http://api.trakt.tv/shows/trending.json/5d7588c188eeea0074b8d2664d12fffc");
$result = json_decode($response, true);
echo $result['title'][0];
echo "<br>";
echo $result['network'][0];
echo "<br>";
echo $result['air_day'][0];
echo "<br><img style='width:200px;' src='";
echo $result['images'][0]['poster'];
echo "'>";
?>
Ain't working. I don't know why.
I use the the trakt.tv shows API.
Write
echo $result[0]['title'];
instead of
echo $result['title'][0];
Besides, PHP's echo function will print integers and strings, but will fail with array-alike structures. You could use var_dump or var_export instead. Thanks to them, you could scan the structure and you wouldn't ask this question ;)

Categories