PHP function and HTML output parse error [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There is something wrong with this code, I cant make it go trough... the situation is that
the difficulty-meter needs to be checked , if it is filled, execute a mix of HTML and PHP. I'm learning php and got stuck in this functions...
Im using WP and WPML to translate that's why I add the -__e('Difficulty', 'projectname')': as a string. I get a error ,
Parse error: syntax error, unexpected 'tpage' (T_STRING), expecting ',' or ';' in /homepages/46/d448593520/htdocs/wp-content/themes/site/document.php on line 218
My full code is
<?php
// CHECK IF DIFFICULTY FIELD EMPTY
$diffcheck = get_post_meta($post->ID, 'wpcf-difficulty-meter', true);
if ( $diffcheck) {
echo "<ul class="tpage-list">
<li>'
-__e('Difficulty', 'projectname')':
'</li><li>'
types_render_field('difficulty-meter', array('output'=>'html','class'=>'tpage-difficulty'))
'</li></ul>";})
}
else {
// Show Nothing
}
// END
?>

The syntax highlighting shows your error: it's a quoting issue. You have to escape your double quotes inside of your string:
echo "<ul class=\"tpage-list\">
or use single quotes:
echo "<ul class='tpage-list'>

To complete what John Conde said, this part make no sense:
echo "<ul class="tpage-list">
<li>'
-__e('Difficulty', 'projectname')':
'</li><li>'
types_render_field('difficulty-meter', array('output'=>'html','class'=>'tpage-difficulty'))
'</li></ul>";})
Lets go through it:
// You have to keep track of concatenation. You started the string with ", you can't use it except to end string (or if you use \ like John Conde said)
// After the li: it seem like you wanted to add the value of the Difficulty thing, so you have to end your string and concaten it with a .
// Back to the strings and concaten it again to add the type render, since the string is now started with a ', I use a ' to end it
echo "
<ul class='tpage-list'>
<li>".-__e('Difficulty', 'projectname').':</li>
<li>'.
types_render_field('difficulty-meter', array('output'=>'html','class'=>'tpage-difficulty'))
.'</li>
</ul>';
I don't really see why you had a }) at the end, so I took it out

Related

Notice: Array to String Conversion while taking form inputs using $_POST[] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have the form input like this. But it is showing
Notice: Array to string conversion in C:\xampp\htdocs\testpage\index.php on line 95
<form class='signupform' action='<?php echo htmlspecialchars('".$_SERVER."'['PHP_SELF']);?'> method='post'>
I have seen many posts around here, but could not solve it.
From your code:
<?php echo htmlspecialchars('".$_SERVER."'['PHP_SELF']);?'>
What you are doing is:
htmlspecialchars(subject)
but subject is made up of a jumble of string and variable references, so to start with the Array ($_SERVER) contains the element(s) you want to work on, which are denoted by the key in the square brackets (['PHP_SELF']). But what you have is a concatination . and a couple of quotes inbetween the two so what you are doing is
htmlspecialchars(array + quote + string )
which is clearly and obviously invalid.
So to fix it, you remove the excess quote marks and remove the concatenations between the array and it's key indicator.
htmlspecialchars('".$_SERVER['PHP_SELF'])
This is better but still not there yet, you now have to tidy up the other quotes as because your function doesn't contain any string (it's just the array variable you're working on here), you do not need any quotes in your code:
htmlspecialchars($_SERVER['PHP_SELF'])
So to wrap up a long post about a very small issue, you would correct with this replacement to your original code:
action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>'>
It is also worth noting that PHP_SELF can be easily abused and should not be used in this context, better to use another similar process such as $_SERVER['SCRIPT_NAME']
You must keep track of your open quotes and try and avoid mixing quotes. You should also keep track of properly closing your PHP code with ?> as your original code you forgot the > so the HTML was being interpreted as PHP by the server.
Use
<form class="signupform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
or
<form class="signupform" action="" method="post">

HTML in PHP (IMG SRC with variable as URL) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to display an image using a variable "$images" that contains the URL parsed form an API.
This is my code:
echo "<td>""<img src='",$image,"'>""</td>\n";
I assume there is a typo I cannot detect because I get a blank screen when running this.
echo "<td>"."<img src='".$image."'>"."</td>";
Or
echo "<td><img src='".$image."'></td>";
You were missing the dots/commas after/before the td tags
use . not ,
<img src='".$image."'>
PHP requires that you chain your strings together using a .
E.g.
echo 'Test' . ' ' . 'Hello'; // Test Hello
Or simply :
echo "<td><img src='$image'></td>";
Check documentation.

Parse error: syntax error, unexpected 'filename' (T_STRING) in C:\xampp\htdocs\php\dir.php on line 9 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
**php program for reading filenames and filetypes from a dir.**
it shows the syntax error :
Parse error: syntax error, unexpected 'filename' (T_STRING) in C:\xampp\htdocs\php\dir.php on line 9
<?php
$dir="C:\xampp\htdocs\php\";
if(is_dir($dir))
{
if($dirref=opendir($dir))
{
while(($file=readdir($dirref))!==false)
{
echo "filename : $file : filetype: ".filetype($dir.$file)."\n";
}
closedir($dirref);
}
}
?>
It appears you've escaped the closing quotes on your $dir= statement. I don't use php in a Windows environment, so I don't know if you can convert all your \ to /, but if not, you'll want to escape all the \:
$dir="C:\\xampp\\htdocs\\php\\";
Or use single quotes, which disables escape sequences:
$dir='C:\xampp\htdocs\php\';
Parse errors often indicate that there is a syntax (or string in this case) error in your code, BEFORE the error actually reported by the parser.
The error is on your first line: $dir="C:\xampp\htdocs\php\";
The backslash escapes the quotes.
Escape the backslashes in your code to make it work, like so:
$dir="C:\\xampp\\htdocs\\php\\";
$dir="C:\xampp\htdocs\php\";
Just remove the last '\' and make it
$dir="C:\xampp\htdocs\php";
It will Work..!
Change this:
$dir="C:\xampp\htdocs\php\";
to
$dir="C:\\xampp\\htdocs\\php\\";

php unexpected 'meta_value' (T_STRING) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
im getting this error but cannot figure out why
while($info = mysql_fetch_array( $data ))
{
echo itg_fetch_image('.$info['meta_value'].'); //the line giving error
Print "<tr>";
Print "<th>ID:</th> <td>".$info['meta_id'] . "</td> ";
Print "<th>VALUE:</th> <td>".$info['meta_value']. "</td> ";
Print "<th>DONE:</th> <td>YES</td> ";
}
if i comment out this line it shows fine in the value td, ive tried everthing. taking the dots out the taking the comments out then adding "". itg_fetch_image is http://www.intechgrity.com/automatically-copy-images-png-jpeg-gif-from-remote-server-http-to-your-local-server-using-php/#
the code is ment to be echo itg_fetch_image('url') and all meta_values return a url string
The quotes on the echo line are causing the problem.
It should look like this:
echo itg_fetch_image($info['meta_value']);
it seems that itg_fetch_image() is a function
try this:
echo itg_fetch_image($info['meta_value']);
you can just pass variable . do not need to use quotes.
You must write it as
echo itg_fetch_image($info['meta_value']);

Browser changing the markup and breaking my CSS [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have in a foreach loop:
echo "<span style=\"" . myCss($value) . "\">lol</span>";
Which turns into (in source):
<span style="">lol</span>color: #999999;background-color: transparent;font-weight:normal;text-decoration: none;<span style="">...
Why? how to prevent the browser? Same for Chrome and Firefox. Note there is a reason for it being in-line, an I want to avoid doing it via javascript.
Try this
echo "<span style='" . myCss($value) . "'>lol</span>";
How about a little separation of PHP and HTML:
<span style="<?php echo myCss($value); ?>">lol</span>
Notice I encapsulate the PHP within the quotes, rather than echo the entire line. In a foreach loop it would look something like:
<?php
foreach($array as $key => $value){
?>
<span style="<?php echo myCss($value); ?>">lol</span>
<?php
}
?>
This separation of PHP and HTML has been the standard practice everywhere that I have worked, and I personally find it to be much more transparent.
Without seeing your function and the values of the variables, I an only assume that there are characters in the echoed result that mess up the html. You should always use htmlspecialschars() when you output to html:
echo "<span style=\"" . htmlspecialschars(myCss($value)) . "\">lol</span>";
Although you would probably use it in your function.

Categories