?
Solved

PHP Syntax Issue

Posted on 2012-08-31
15
Medium Priority
?
599 Views
Last Modified: 2013-11-19
Having a problem with the below code...

this is just a sample but i am trying to figure out crrect format Syntax


      if ( isset( $_GET['simple'] ) )
      {  
            $txt = $_GET['simple'];
      
      }
      else
      {   
            $txt = "No Data Provided";
      }
0
Comment
Question by:AlexPonnath
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38355026
First, remember to validate any data you're using: for instance, be sure $_GET['simple'] is a value of expected type, in thas expected range, and so on

Second, what is the issue? Your syntax is correct...
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38355030
You could do this with the ternary operator:

$txt = (isset($_GET['simple'])) ? $_GET['simple'] : "No Data Provided";
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38355048
syntax seems to be correct from what I can see ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AlexPonnath
ID: 38355084
I would like to use a codeing style similar to what i have provided since it is cleaner to read then

$txt = (isset($_GET['simple'])) ? $_GET['simple'] : "No Data Provided";

The error i am getting that my editor complains about

Multiple annotations found at this line:
      - syntax error, unexpected '$txt'
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38355131
That seems to be a problem with your editor (eclipse?) rather then your code. It is correct PHP syntax and should render absolutely fine in a browser.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38355163
Yes, Eclipse and Netbeans sometimes sign errors that is not errors, or simply they could be an error. In your case, your editor simply says that $txt variable is not declared: try to add at the top of the script this line:

$txt = '';

Anyway, your code will work fine.

Cheers
0
 

Author Comment

by:AlexPonnath
ID: 38355167
I am using Zend Studio and if i run the code my Webserver doesnt like it either..

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38355196
Are you sure all the other parts of your code are correct?
Do you get any errors in your server log?
Is your server configured properly to run PHP?
0
 

Author Comment

by:AlexPonnath
ID: 38355212
First this is the only code on page so there should be nothing which causes this error other then code.
If i remove bothlike $txt = "No Data Provided";

code runs fine but logicly returns blank page.

if i put an echo "That sucks" instead of the $txt = "No Data Provided"; i still get an error

i i place the echo outside the if statement it works just fine

all that lets me to belive there is something wrong with syntax
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38355283
Baffling. It all looks fine! I would try a very simple statement and work from there :)

if (true) {
      echo "True";
} else {
      echo "False";
}
0
 

Author Comment

by:AlexPonnath
ID: 38355297
That works just fine and returns a true
0
 
LVL 45

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 38355329
Now build on it until it breaks :)

if (true) {
      $txt = "True";
} else {
      $txt = "False";
}

echo $txt;
0
 

Author Comment

by:AlexPonnath
ID: 38355377
Now thats one i wont get...

<?php

if ( isset( $_GET['simple'] ))
      {
      $txt = $_GET['simple'];
      }
else
      {
      $txt = "False";
      }




if ( isset( $_GET['simple'] ) )
{
      $txt2 = $_GET['simple'];

}
else
{
      $txt2 = "No Data Provided";
}


echo $txt;
echo $txt2;

?>

which includes your code modified and my code work now both without issues in editor and on server..
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38355401
The original code works fine here.  A test page for you.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Syntax Check</title>
</head>
<body>
<h1>PHP Syntax Check</h1>
<a href="PHPsyntax.php">PHPsyntax.php</a><br />
<a href="PHPsyntax.php?simple=I%20don%27t%20know.">PHPsyntax.php?simple=I don't know.</a><br /><br />

<?php 
if(isset($_GET['simple']))
	{  
	$txt = $_GET['simple'];
	}
else
	{   
	$txt = "No Data Provided";
	}
echo $txt;
 ?>
</body>
</html>

Open in new window

0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38355403
that's just weird :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question