Jump to content

T_CONSTANT_ENCAPSED_STRING


iwato
 Share

Recommended Posts

QUESTION ONE: What is a T_CONSTANT_ENCAPSED_STRING?QUESTION TWO: Why might it appear as a parsing error when the following block of code is run?QUESTION THREE: How does one go about finding a definition of the above constant, and for the future other such constants?ERROR MESSAGE: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in . . . .BACKGROUND1) The file "practice.txt" and the file containing the PHP code provided below coexist in the same folder.2) The command line to which the error message points has little or no relevance to the statement found in the line. I know this because I have entered other statements in the same line. 3) The following block of code is the only PHP code in the entire php file.

		<?php			$filename = "practice.txt";			$somecontent = "Add this text to the file\n";			if (is_writable($filename)) {			    if (!$handle = fopen($filename, 'a')) {			         echo "Cannot open file ($filename)";			         exit;			    }			    if (fwrite($handle, $somecontent) === FALSE) {			        echo "Cannot write to file ($filename)";			        exit;			    }			    echo "Success, wrote ($somecontent) to file ($filename)";			    fclose($handle);			}			else {			    echo "The file $filename is not writable";			}		?>

Roddy

Link to comment
Share on other sites

The line number always has some relevance, and it would help if you did tell us it. It could be the line before, or the line after... but it will be somewhere around there.As for the error, the T indicates that it is a token. A list of tokens can be found at http://php.net/manual/en/tokens.php. Looking there, it shows that the error means that it found a string in an unexpected place. However, there is no error in the code you have posted, so maybe posting all the code and the entire error message may help.

Link to comment
Share on other sites

The line number always has some relevance, and it would help if you did tell us it. It could be the line before, or the line after... but it will be somewhere around there.
Based upon what you have written I can well appreciate your desire to see the entire error message, but consider the two separately run blocks of code first. The first produces the desired number 1. The second produces the same error message that I have already provided and am providing again in full.Line 37 by the way is the line just after the statement: echo $file_exists($filename);
<?php	 echo $file_exist('practice.txt');?>

and

<?php	 $filename = 'practice.txt';	 echo $file_exist($filename);?>

Error Message: // Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMPL10XJMP4.php on line 37

As for the error, the T indicates that it is a token. A list of tokens can be found at http://php.net/manual/en/tokens.php. Looking there, it shows that the error means that it found a string in an unexpected place.
I found this very helpful. Slowly, but surely, I am learning how to use the PHP Manual. The code for the entire PHP webpage is provided below -- a snapshot of how I am studying PHP, one function and group of functions at a time.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>PHP Filesystem - The fwrite( ) Function</title><style type="text/css"><!--body {	font: 100% Verdana, Arial, Helvetica, sans-serif;	background: #666666;	margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */	padding: 0;	text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */	color: #000000;}.oneColElsCtr #container {	width: 46em;	background: #FFFFFF;	margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */	border: 1px solid #000000;	text-align: left; /* this overrides the text-align: center on the body element. */}.oneColElsCtr #mainContent {	padding: 0 20px; /* remember that padding is the space inside the div box and margin is the space outside the div box */}--></style></head><body class="oneColElsCtr"><div id="container">  <div id="mainContent">	<h1>PHP Filesystem - The fwrite( ) Function</h1>		<?php			$filename = 'practice.txt';			$somecontent = "Add this text to the file\n";			if (is_writable($filename)) {			    if (!$handle = fopen($filename, 'a')) {			         echo "Cannot open file ($filename)";			         exit;			    }			    if (fwrite($handle, $somecontent) === FALSE) {			        echo "Cannot write to file ($filename)";			        exit;			    }			    echo "Success, wrote ($somecontent) to file ($filename)";			    fclose($handle);			}			else {			    echo "The file $filename is not writable";			}		?>	<p></p>	<!-- end #mainContent --></div><!-- end #container --></div></body></html>

Link to comment
Share on other sites

Unfortunately, I am still unable to reproduce the error with the code you have given. :) Are you sure that is what you are running? I don't see any calls to file_exists) in it.Also note that if you make a call to $function(), it will try to resolve $function and make a call to the function with the name similar to the value of said variable.

Link to comment
Share on other sites

Unfortunately, I am still unable to reproduce the error with the code you have given. :) Are you sure that is what you are running? I don't see any calls to file_exists) in it.
Actually, I am not surprised that you were unable to reproduce the error, because I do not believe that the error has anything to do with the code per se. Also, please read carefully what I wrote in my last entry; each of the three sets of code displayed was run separately. In separate trials I substituted the PHP code in the last set with the PHP code from the first and second sets, respectively.
Also note that if you make a call to $function(), it will try to resolve $function and make a call to the function with the name similar to the value of said variable.
I do not understand this statement. Perhaps you could provide an example of what you mean and show how it may be relevant to each of the three sets of code to which you meant it to apply.Roddy
Link to comment
Share on other sites

Actually, I am not surprised that you were unable to reproduce the error, because I do not believe that the error has anything to do with the code per se. Also, please read carefully what I wrote in my last entry; each of the three sets of code displayed was run separately. In separate trials I substituted the PHP code in the last set with the PHP code from the first and second sets, respectively.
Well, an error results from code that you run. Closer examination of your environment will probably turn up the problem - for example, maybe you are using a strange character set?
I do not understand this statement. Perhaps you could provide an example of what you mean and show how it may be relevant to each of the three sets of code to which you meant it to apply.
function a() {	return "b";}$c = "a";echo $c(); //prints "b".

Link to comment
Share on other sites

file_exist or file_exists ? One is a function. One is not.This looks wrong:if (!$handle = fopen($filename, 'a')) {Note that ! takes precedence over =That means that !$handle evaluates to TRUE or FALSE before the assignment. So you are trying to assign the return value of fopen to FALSE or TRUE, which is not what you mean. Try:if ( ($handle = fopen($filename, 'a') ) === FALSE ) {or more simply$handle = fopen($filename, 'a');if (!$handle) {

Link to comment
Share on other sites

This looks wrong: if (!$handle = fopen($filename, 'a')) {
OK. So, I tried the following. No change in the error message.
			   		<?php			$filename = 'practice.txt';			$somecontent = 'Add this text to the file\n';			if (is_writable($filename)) {				$handle = fopen($filename, 'a');			    if (!$handle) {			         echo 'Cannot open file ($filename)';			         exit;			    }			    if (fwrite($handle, $somecontent) === FALSE) {			        echo 'Cannot write to file ($filename)';			        exit;			    }			    echo 'Success, wrote ($somecontent) to file ($filename)';			    fclose($handle);			}			else {			    echo 'The file ($filename) is not writable';			}		?>

What does work is the following:

<?php	 $filename = 'practice.txt';	 echo is_writable($filename);?>

The output is 1.Roddy

Link to comment
Share on other sites

Show us the error message. Is the error still on line 37? Which line is 37?
The error message is the same as before and line 37 corresponds to the following statement: if (is_writable($filename)) {ERROR MESSAGE: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP4043XKHRZ.php on line 37Roddy
Link to comment
Share on other sites

There's nothing wrong with that line, the error is before that line. I actually haven't seen an error in any of the code you've posted so far.
I am beginning to believe that the error is with the MAMP Pro or Dreamweaver. MAMP Pro is not responding to my support requests, and recently I have had trouble uploading to one of my websites using Dreamweaver. It is very frustrating when people with many years of experience can find no error in my code, and I keep receiving the same error message despite numerous modifications.Roddy
Link to comment
Share on other sites

Is the code you posted in post 3 the complete code you're using?What's the story with the filename btw:/Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP4043XKHRZ.php on line 37Is something generating this file for you, or are you typing your code in a text editor and then running it in a browser?

Link to comment
Share on other sites

Good morning, everyone!

Is the code you posted in post 3 the complete code you're using?
In post three I posted three sets of code. Each was run independently of the other and provided the results indicated.
What's the story with the filename btw:/Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP4043XKHRZ.php on line 37Is something generating this file for you, or are you typing your code in a text editor and then running it in a browser?
The file called practice.txt is just an ordinary text file created with TextEdit and saved into my Dreamweaver file folder. The folder has been updated several times.Also, there is nothing unusual about my character fonts. They are standard UTF-8 found on all Apple Computers.Roddy
Link to comment
Share on other sites

In post three I posted three sets of code. Each was run independently of the other and provided the results indicated.
I don't think that answered my question, is the code in post 3 the exact code you are currently running?
The file called practice.txt is just an ordinary text file created with TextEdit and saved into my Dreamweaver file folder. The folder has been updated several times.
I'm talking about this file:/Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP4043XKHRZ.phpDid you name your file TMP4043XKHRZ.php? Is that a random name assigned by something else? If something is generating this file for you then we need to see the contents of that file, we need to see the exact code you are running which produces the error in order to tell you where the error is. All I can say about that error is that there's a string where it's not expecting one. Leaving out a concatenation operator, for example, would produce that error:
$str = 'text'.'text' 'text';--------------------^ missing operator

Link to comment
Share on other sites

I don't think that answered my question, is the code in post 3 the exact code you are currently running?
Those three sets of code were run in order to test whether the file practice.txt would be recognized under different environments. It was recognized in only one of the three environments in which it was tested.The code that I am currently running is:
		<?php			$filename = 'practice.txt';			$somecontent = 'Add this text to the file\n';			if (is_writable($filename)) {				$handle = fopen($filename, 'a');			    if (!$handle) {			         echo 'Cannot open file ($filename)';			         exit;			    }			    if (fwrite($handle, $somecontent) === FALSE) {			        echo 'Cannot write to file ($filename)';			        exit;			    }			    echo 'Success, wrote ($somecontent) to file ($filename)';			    fclose($handle);			}			else {			    echo 'The file ($filename) is not writable';			}		?>

I'm talking about this file:/Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP4043XKHRZ.php
TMP4043XKHRZ.php is generated by Dreamweaver. It is automatically produced when I test my PHP document with Safari. The name is randomly assigned each time a test is run. Please find below the source view of the file named TMP4043XKHRZ.php.
<br /><b>Parse error</b>: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in <b>/Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP6TUBL74S.php</b> on line <b>37</b><br />
Link to comment
Share on other sites

Well, Dreamweaver is most likely generating the file badly then, i.e. what is running is not what you think is running.

Link to comment
Share on other sites

The code that I am currently running is:
That code does not contain the syntax error in the error message, that code runs without issue. Syntax errors are not server-specific, you would never get a syntax error on one server but not another (unless they were two completely different versions of PHP, like PHP4 vs. PHP5; this script doesn't use anything that would have changed though). It really sounds like something that Dreamweaver is doing is breaking the code. Try pasting the code you posted above into a new text document in TextEdit, and save the file as fwrite.php in your server directory, then browse to that file and see what happens, I bet it will work (btw, if the file doesn't exist the error message is just that it's not writable, which is also true).The source you posted is the HTML source from the browser though, I'm interested in the actual PHP file. If that random filename stays on disk long enough for you to open and copy the code, I would be interested to see that. It might be that Dreamweaver creates the temp file, opens it in a browser, and immediately deletes it, in which case you wouldn't be able to check the PHP code in it. I'm wondering if the code which actually gets written to the file that runs is what you think it is, Dreamweaver might change it somehow.
Link to comment
Share on other sites

It really sounds like something that Dreamweaver is doing is breaking the code. Try pasting the code you posted above into a new text document in TextEdit, and save the file as fwrite.php in your server directory, then browse to that file and see what happens, I bet it will work (btw, if the file doesn't exist the error message is just that it's not writable, which is also true).
In a previous experiment and entry to this thread I explained that is_writable(), when run by itself, produces a 1. The file practice.txt is writable.Please find copies of both the php and text files at the following two URLs: fwrite.php and practice.txt, respectively.By the way, I have now reinstalled by system folder as well as Dreamweaver and the entire suite of Adobe CS3's shared components. The only piece of software in play that I have still have yet to reinstall is MAMP Pro.Roddy
Link to comment
Share on other sites

Umm, we're not saying that your CS3 installation is broken - just that when Dreamweaver generates the PHP file, it does so badly, introducing an error. Under your system, fwrite.php is not the file that is executed - it is just part of a DW-created document. What does /Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP6TUBL74S.php contain?

Link to comment
Share on other sites

What does /Applications/MAMP/htdocs/php-practice/functions/filesystem/fwrite/TMP6TUBL74S.php contain?
I have already provided that information in Post #16Roddy
Link to comment
Share on other sites

I thought that was what was in fwrite.php?
Examine the entire post -- both the CODE and QUOTE. The latter contains the source code for the randomly generated file.Roddy
Link to comment
Share on other sites

Err, that's the generated source though - where's the PHP for it?It is the PHP code in TMP4043XKHRZ.php that is causing the error.

Link to comment
Share on other sites

Err, that's the generated source though - where's the PHP for it?It is the PHP code in TMP4043XKHRZ.php that is causing the error.
Yes and no. I have just deleted and retyped the entire code in Dreamweaver. It ran.Needless to say, I am very frustrated by the outcome of these events. Why? This means that Dreamweaver permits hidden characters in its code section. And to think, that I copied the code into TextEdit from the PHP website, reformatted it as text, and saved it before copying it into Dreamweaver. I am very disheartened.Roddy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...