October 22nd, 2013, 06:00 AM
-
PHP includes with echo help
I am a newbie at PHP and yes some people will probably laugh at my newbie question.
So far I have learnt that you can use PHP includes to retrieve data/text from another page and print/display it, which is very useful to me as I only need to have one header, footer page etc. and I can just use echo to display/print the data.
What I would like to do is that I have pages where I have different content in the one page, but I want to include parts of this content on different sections of the page. I have tried this and it works great:
The page with the data/text/html to include:
PHP Code:
<?php
$title='This page has a name';
$description='Info that describes this page';
?>
The page that I wan't the data/text to appear on:
PHP Code:
<table border="1" width="100%">
<tr>
<td width="100%"><?php include 'this_page.php';
echo "$title" ?></td>
</tr>
<tr>
<td width="100%"><?php include 'this_page.php';
echo "$description" ?></td>
</tr>
</table>
This works, but the only problem I face is that the text/data I want to include can also be html code or text that has " or ' or + or $ symbols in it and obviously this gives an error. I understand that because this html/symbols appears in between the <php> tag that this error will occur, is there a way around this?
And also is using echo the best approach or is there a simpler/more efficient way of going about displaying content on different parts of the page?
I did look in the stickies and tried a search but couldn't find anything relevant to my question.
Thank you for any help
October 22nd, 2013, 06:15 AM
-
i am not sure why if you have
$title = '$+something'; giving you errors?
point 1: look at your include statements?
point 2: redundant "" around echo $title..
point 3: just do <?= $title ?>
Comments on this post
October 22nd, 2013, 06:39 AM
-
Originally Posted by paulh1983
i am not sure why if you have
$title = '$+something'; giving you errors?
point 1: look at your include statements?
point 2: redundant "" around echo $title..
point 3: just do <?= $title ?>
Thanks for the reply paulh1983
Thanks I will try omitting the "" around the $title in the echo.
October 22nd, 2013, 08:14 AM
-
You don't need to include something multiple times. You're also including "this_page" and I'm worried you have a page which includes itself recursively.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you
asked a bad question or you're a
Help Vampire. Trying to argue intelligently? Please
read this.
October 23rd, 2013, 05:16 AM
-
Originally Posted by paulh1983
i am not sure why if you have
$title = '$+something'; giving you errors?
point 1: look at your include statements?
point 2: redundant "" around echo $title..
point 3: just do <?= $title ?>
I have changed the code to this now as per you and ManiacDan suggestions, I know have:
PHP Code:
<table border="1" width="100%">
<tr>
<td width="100%"><?php include 'this_page.php';
echo $title ?></td>
</tr>
<tr>
<td width="100%"><?php
echo $description ?></td>
</tr>
</table>
Works good. But I have found out what causes the error and I apologise as it's not $ + or any other symbols that I first though was causing the "Parse error: syntax error, unexpected" it's because some of the text often has a ' in it. I have learnt that you can add a backslash / just before the ' but I have a lot of different files that I would need to change this on, is there a way to escape the ' dilemma?
October 23rd, 2013, 05:24 AM
-
Originally Posted by ManiacDan
You don't need to include something multiple times. You're also including "this_page" and I'm worried you have a page which includes itself recursively.
Thank you for the reply, I have taken your advice and changed it.
I probably haven't explained clearly what I am trying to achieve, it's simple in theory but harder for me to explain.
I have a php file that has different parts of text & html in it e.g.:
some text/html here
some more text html here
more text/html here
I want to include these different parts of information on another page which is like the master page which has all my design/template on it, I am trying to get those different parts of text/html to appear/print on different parts of the master page just like you would with a header and footer (top and bottom respectively) I can get this to work no problems with the above code, but the only problem I get an error whenever a ' is present in the text or html and I am guessing that this is occurring as PHP interprets the ' being part of the php code?
October 23rd, 2013, 06:32 AM
-
echoing string with ' shouldnt be a problem. It will/should only be a problem IF you are inserting it into a database using MYSQL
October 24th, 2013, 05:40 AM
-
Originally Posted by paulh1983
echoing string with ' shouldnt be a problem. It will/should only be a problem IF you are inserting it into a database using MYSQL
Thanks for the reply.
Are you sure about this?
This is exactly how I have it set out and still get the error:
This is the content I wan't to include:
PHP Code:
<?php
$title='This is my text's';
?>
This is the php file for the above include:
PHP Code:
<?php include 'more.php';
echo $title ?>
Error message: Parse error: syntax error, unexpected T_STRING in /more.php on line 2
If I add a backslash \ before the ' or remove the ' then there is no error, it works fine.
October 24th, 2013, 05:52 AM
-
You can't have a raw single quote in a single quoted string. How is the PHP parser supposed to distinguish between a literal single quote and a single quoted denoting the end of the string?
You have three options:
- Use a double quoted string. Then single quotes are no problem (but double quotes are).
- Use backslashes (which you don't want).
- Use heredocs. This is cumbersome, but it makes sense if you have a lot of single and double quotes mixed.
October 24th, 2013, 06:11 AM
-
Originally Posted by Jacques1
You can't have a raw single quote in a single quoted string. How is the PHP parser supposed to distinguish between a literal single quote and a single quoted denoting the end of the string?
You have three options:
- Use a double quoted string. Then single quotes are no problem (but double quotes are).
- Use backslashes (which you don't want).
- Use heredocs. This is cumbersome, but it makes sense if you have a lot of single and double quotes mixed.
Thanks for the reply Jacques1.
I had read about using the backslashes and I agree this is something I don't wan't to do.
What are most people in the PHP world doing when they wan't to include content? Is this method the best way of setting out parts of text/data on a page. All I wan't is to have the master/main page which has all my design on it, then just have the seperate php files which have my text/html etc. and include them on the master/main page in it, then I can just call up each section such as http://www.example.com/name?=this_page
The problem is that a lot of the text has " and ' in it.
October 24th, 2013, 07:06 AM
-
In your example, you don't any php tags at all
eg
include-me.php
Code:
<h3>Raw HTML/h3>
<p>no php tags</p>
including:
PHP Code:
<?php
include 'include-me.php';
?>
One way to think of it is this: The parser sees that there is no opening <?php in the included file and auto closes the php block until a new opening <?php is found or the end of the file is reached and control flows back to the original script...where the php is opened again
So, when people say that including files is like copy and paste...just remember it's not exactly the same.
If you want an include to set variables, then the variables that are set must follow the normal PHP rules, so it would be
PHP Code:
<?php
$title = "A double quoted string with ' inside is fine";
$content = 'a single quoted string with " inside is fine';
and if people start talking to you about "single quotes being faster" or anything like that, here's a good article to read that says *******s to all that and have some common sense:
http://nikic.github.io/2012/01/09/Di...ance-Myth.html
Last edited by Northie; October 24th, 2013 at 07:12 AM.
October 24th, 2013, 08:32 AM
-
yeah its not the echo $string that is a problem (which i mentioned before).. it is when you are defining/creating them.. so do what above have said..
October 24th, 2013, 08:42 AM
-
Also, get an editor with "syntax highlighting".
See how the code excerpts on this thread have coloured all the strings red? Now look at your example in reply #8 and see how the colours look a bit odd? This is a good hint that your quotes don't match up properly
Editors such as textpad, notepad++ and IDEs (eg netbeans, aptana etc) have this feature for many different types of file (php, html, css, javascript etc)
October 25th, 2013, 06:21 AM
-
Thanks all for the replies.
I understand that html should be separate from any PHP to avoid such problems, especially when quotes are used
I also understand that when you have " " you can't use another " inside and vice versa with a single quote ' '
My biggest problem is that all of my content (and there are hundreds of different pages) contains html and text together, which means that I have both double and single quotes together in the same "blocks" that I want to include, so for example I wan't to include this:
<table id="main" cellspacing="0">
<tr>
<td class="info-cell">
This information, contains several single quotes ' and many " double quotes ".
</td>
</tr>
</table>
So if I use either of these it won't work either way because both examples would contain both single and double quotes.
PHP Code:
<?php
$title = "A double quoted string with ' inside is fine";
$content = 'a single quoted string with " inside is fine';
I wish I could go back in time and stop myself doing my pages like this :-(
October 25th, 2013, 06:29 AM
-
do any of your hundreds of pages contain any PHP?
if they don't then it doesn't matter