Jump to content

T_ENCAPSED_AND_WHITESPACE error


Lodius2000

Recommended Posts

I am really new to php, working through a book and think I have enough knowledge to make a program that converts rgb values to Hex. I am copying the code in my book of how to display default values in a text box but i keep getting this error, i think it is more with the syntax of the concatination than the actual wording of the code.

 

I am getting an error that reads

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in public_html/phpexercises/testfile.php on line 54

 

there is a comment in the code before line 54, when i fix 54 i will assume that I need to fix 55 and 56 too since the code is the same

 

thanks in advance for any help given

 

<?php

if($_POST['_submit_check']){
if($form_errors = validate_form()){
show_form($form_errors);
} else {
show_form();
process_form();
}
} else {
show_form();
}

function process_form(){

sprintf('<body bgcolor="#%02x%02x%02x">', $_POST['red_value'], $_POST['green_value'], $_POST['blue_value']);


sprintf('<div>The Hex conversion is #%02x%02x%02x</div>', $_POST['red_value'], $_POST['green_value'], $_POST['blue_value']);
}

function show_form($errors = '') {
if ($_POST['_submit_check']) {
$defaults= $_POST;
} else {
$defaults = array('red_value' => 0,
'green_value' => 0,
'blue_value' => 0);
}

if ($errors){
print 'Please correct these errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}

print<<<_HTML_
<html>
<head>
<style type="text/css">
div {
padding: 10;
position-absolute: 0 0;
background-color: #ffffff;
}
</style>
</head>
<div>
<fieldset>
<legend>
Hexidecimal Converter
</legend>
<form method="POST" action="$_SERVER[php_SELF]">
// line 54 is below this comment
Red: <input type="text" name="red_value" value="'.htmlentities($defaults['red_value']).'">
Green: <input type="text" name="green_value" value="'.htmlentities($defaults['red_value']).'">
Blue: <input type="text" name="blue_value" value="'.htmlentities($defaults['red_value']).'">
<br /><br />
<input type="submit" name="Convert" value="Convert">
<input type="hidden" name="_submit_check" value="1">
</form>
</fieldset>
</div>
_HTML_;
}

function validate_form(){
if(strlen(trim($_POST['red_value'])) > 255 || strlen(trim($_POST['red_value'])) < 0){
$errors[] = 'The red value entered must be between 0 and 255';
}

if($_POST['red_value'] != strval(intval($_POST['red_value']))){
$errors[] = 'RED: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['red_value'] > 255 || $_POST['red_value'] < 0){
$errors[] = 'The red value entered must be between 0 and 255';
}

if(strlen(trim($_POST['green_value'])) > 255 || strlen(trim($_POST['green_value'])) < 0){
$errors[] = 'The green value entered must be between 0 and 255';
}

if($_POST['green_value'] != strval(intval($_POST['green_value']))){
$errors[] = 'GREEN: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['green_value'] > 255 || $_POST['green_value'] < 0){
$errors[] = 'The green value entered must be between 0 and 255';
}

if(strlen(trim($_POST['blue_value'])) > 255 || strlen(trim($_POST['blue_value'])) < 0){
$errors[] = 'The blue value entered must be between 0 and 255';
}

if($_POST['blue_value'] != strval(intval($_POST['blue_value']))){
$errors[] = 'BLUE: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['blue_value'] > 255 || $_POST['blue_value'] < 0){
$errors[] = 'The blue value entered must be between 0 and 255';
}

return $errors;
}

?>

 

(edited by kenrbnsn to add the


tags)

Link to comment
https://forums.phpfreaks.com/topic/98264-t_encapsed_and_whitespace-error/
Share on other sites

The "print<<<_HTML_" is the start of your Heredoc (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

 

It should be:

print = <<<_HTML_

 

 

 

no... you will get an error if you try that... you can't assign a construct a value...

I just took out the HEREDOC, escaped output PHP for that section and then back in. I also added indentation and corrected you output function (which wasn't echoing anything):

<?php

if($_POST['_submit_check']){
if($form_errors = validate_form()){
	show_form($form_errors);
} else {
	show_form();
	process_form();
}
} else {
show_form();
}

function process_form(){
echo sprintf('<body bgcolor="#%02x%02x%02x">', $_POST['red_value'], $_POST['green_value'], $_POST['blue_value']);
echo sprintf('<div>The Hex conversion is #%02X%02X%02X</div>', $_POST['red_value'], $_POST['green_value'], $_POST['blue_value']);
}

function show_form($errors = '') {
if ($_POST['_submit_check']) {
	$defaults= $_POST;
} else {
	$defaults = array('red_value' => 0,
	'green_value' => 0,
	'blue_value' => 0);
}

if ($errors){
	print 'Please correct these errors: <ul><li>';
	print implode('</li><li>', $errors);
	print '</li></ul>';
}
?>
<html>
<head>
<style type="text/css">
div {
padding: 10;
position-absolute: 0 0;
background-color: #ffffff;
}
</style>
</head>
<div>
<fieldset>
<legend>
Hexidecimal Converter
</legend>
<form method="POST" action="<?php echo $_SERVER[php_SELF]?>">
Red: <input type="text" name="red_value" value="<?php echo htmlentities($defaults['red_value'])?>">
Green: <input type="text" name="green_value" value="<?php echo htmlentities($defaults['red_value'])?>">
Blue: <input type="text" name="blue_value" value="<?php echo htmlentities($defaults['red_value'])?>">
<br /><br />
<input type="submit" name="Convert" value="Convert">
<input type="hidden" name="_submit_check" value="1">
</form>
</fieldset>
</div>
<?php }

function validate_form(){
if(strlen(trim($_POST['red_value'])) > 255 || strlen(trim($_POST['red_value'])) < 0){
	$errors[] = 'The red value entered must be between 0 and 255';
}

if($_POST['red_value'] != strval(intval($_POST['red_value']))){
	$errors[] = 'RED: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['red_value'] > 255 || $_POST['red_value'] < 0){
	$errors[] = 'The red value entered must be between 0 and 255';
}

if(strlen(trim($_POST['green_value'])) > 255 || strlen(trim($_POST['green_value'])) < 0){
	$errors[] = 'The green value entered must be between 0 and 255';
}

if($_POST['green_value'] != strval(intval($_POST['green_value']))){
	$errors[] = 'GREEN: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['green_value'] > 255 || $_POST['green_value'] < 0){
	$errors[] = 'The green value entered must be between 0 and 255';
}

if(strlen(trim($_POST['blue_value'])) > 255 || strlen(trim($_POST['blue_value'])) < 0){
	$errors[] = 'The blue value entered must be between 0 and 255';
}

if($_POST['blue_value'] != strval(intval($_POST['blue_value']))){
	$errors[] = 'BLUE: Please use a NUMBER between 0 and 255, letters and symbols are not valid';
}

if($_POST['blue_value'] > 255 || $_POST['blue_value'] < 0){
	$errors[] = 'The blue value entered must be between 0 and 255';
}

return $errors;
}

?>

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.