PHP include() gets error [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have 35.php that includes common menu for my website. 36.php page should display the menu by using include()
Why doesn't this work?
My error:
35.php is shown below;
<?php
echo "Home
PHP
HTML
CSS <br />";
?>
36.php is shown below;
enter code here
<html>
<body>
<?php include("35.php"); ?>
<p>About Menu is imported from 35.php file</p>
</body>
</html>

You missused the ". They include the string but you also used them for the href. But that closes the string
echo "<a href="
This is where the String ends for PHP and it wants either a ; fo finish the statement or a . to connect it to another string.
So this is what you could to to solve it:
echo 'Home
PHP
HTML
CSS <br />';
With switching the "to a ' The string symbol is now the ' and therefor the " can be used int he string normaly.
Otherwise you could also escape the " like this \" inside the string:
echo "Home ...

Related

Put php value in the end of a link [duplicate]

This question already has answers here:
How to echo inside html tags
(6 answers)
Closed 5 years ago.
Script 1.
add_action('asgarosforum_after_post_author', 'my_function_asgaros_cabinet', 10, 1);
function my_function_asgaros_cabinet($author_id) {
$userData = get_userdata($author_id);
if (!empty($userData) && !empty($userData->description)) {
echo $userData->description;
}
}
Script 2.
I want to put this in script 1 and put ''description'' in the end of that link.
How to do that and whats the right way/code? Cause i think my code is wrong.
<iframe src="http://test.com/me.php?sid='echo $userData->description'"></iframe>
Try with adding the php tag:
<iframe src="http://test.com/me.php?sid=<?php echo $userData->description?>"></iframe>
or
You can try to write the all iframe tag with php:
echo '<iframe src="http://test.com/me.php?sid='. $userData->description .'"></iframe>'

PHP loop is not throwing the correct link [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
for my issue i'll try to be as brief as possible
what am trying to do is reference a page with a specific id in the HTML anchor link with PHP as the below
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php echo $linkName?><br>
and it works fine
now what am trying to do is to make the $id part more dynamic by making looping the number from 1 to 10 and also providing 10 links
the code is
</head>
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=<?php echo $i;?'>Link1</a>";
};
?>
</body>
however what i did notice as the below images indicates when i hover on the links i noticed that i refer to a strange link
and when i cliched on it it takes me to the following link with an id that i did not want as below
http://localhost/PHP_Course/secondPage.php?id=%3C?php%20echo%201;?
i tried researching the subject and i tried escaping the quotation but it does not seem to resolve the problem
Any help please ??
<?php and ?> tags indicate to the PHP preprocessor that anything inside them is code and needs to be parsed, everything outside is just text PHP doesn't touch.
Inside the <?php tag, "<?php" string has no special meaning, so is printed. You do not need to open and close tags all the time, try this:
</head>
<body>
<?php
$linkName = "Second Page";
$id = 5;
for ($i = 0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=$i;'>Link1</a>";
};
?>
</body>
You're echoing a string in PHP, and using <?php... inside that string.
Solution:
echo "<a href='secondPage.php?id=" . $i . "'>Link1</a>";
id=$i will also work, because you can include variables directly in double-quoted strings.
You're echoing the PHP code itself as a string. You don't need to put PHP code inside of PHP code. Just concatenate the values you want to echo:
echo 'Link1';
because you already started an echo statement so you don't need to add another PHP starting and ending tags. just check my code below and try it.
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=".$i."'>Link1</a>";
} ;
?>

How can i display form data in echo php? [duplicate]

This question already has answers here:
How to echo php code inside html which is inside php code in this situation? [duplicate]
(2 answers)
Closed 7 years ago.
How can i display form data in echo php ?
My code is:
<?php
echo "<td align='center' style='vertical-align:middle;'> Bust:
<?php
echo $_POST["bust"];
?>
<br> Waist:
<?php
echo $_POST["waist"];
?>
<br> </td>";
?>
But this doesn't work, please guide me with correct steps.
There are a lot of problems with your code.
You're using " in $_POST["bust"] but you opened echo with " as well, causing a conflict.
You're using <?php instead an echo... which is already PHP. There's no need to do this.
It is best to isolate variable printing from your code. Use string concatenation with . as such:
echo "<td align='center' style='vertical-align:middle;'> Bust: ".$_POST["bust"]."<br> Waist: ".$_POST["waist"]."<br> </td>";

how to display <li></li> tag only after login [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I am trying to display the logout button only if any user is logged in.
following is the code:
<?php
if(isset($_SESSION['user']))
{ echo '<li><a href='logout.php'>Logout</li>'; }
?>
But it is giving error.
Error:Parse error: syntax error, unexpected 'logout' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\index.php on line 42
Help me to display this link.
You can't use unescaped ' characters in string literals delimited by ' characters. Either:
Use " instead
Escape the '
Drop out of PHP mode and just write the HTML as normal template code
Such:
<?php
if(isset($_SESSION['user'])) {
?>
<li><a href='logout.php'>Logout</li>
<?php
}
?>
You are closing your quotes and opening them again. Try this:
<?php
if(isset($_SESSION['user'])) {
echo '<li><a href="logout.php">Logout</li>';
}
?>
Note the double quotes.
Alternatively, escape the quotes like this:
<?php
if(isset($_SESSION['user'])) {
echo '<li><a href=\'logout.php\'>Logout</li>';
}
?>

PHP img alt attribute [duplicate]

This question already has answers here:
CodeIgniter img tag
(3 answers)
Closed 9 years ago.
I want to include an image alt attribute using PHP.
I currently have the following code...
<?php echo img(IMAGEPATH . 'logo.png'); ?>
How would I go about adding the alt attribute - "my awesome alt" - to this image using PHP so that when I go into view source I see something like this for the image?
<img src="http://examplesite.com/img/logo.png" alt="my awesome alt"/>
I am using codeigniter.
<?php echo img(array('src'=>'image/picture.jpg', 'alt'=> 'alt information')); ?>
Try like
<?php echo img(IMAGEPATH . 'logo.png',array('alt'=> 'alt information')); ?>

Categories