• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

PHP Syntax Issue

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
AlexPonnath
Asked:
AlexPonnath
  • 6
  • 5
  • 2
  • +2
1 Solution
 
Marco GasiFreelancerCommented:
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
 
Chris StanyonCommented:
You could do this with the ternary operator:

$txt = (isset($_GET['simple'])) ? $_GET['simple'] : "No Data Provided";
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
syntax seems to be correct from what I can see ...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AlexPonnathAuthor Commented:
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
 
Chris StanyonCommented:
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
 
Marco GasiFreelancerCommented:
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
 
AlexPonnathAuthor Commented:
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
 
Chris StanyonCommented:
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
 
AlexPonnathAuthor Commented:
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
 
Chris StanyonCommented:
Baffling. It all looks fine! I would try a very simple statement and work from there :)

if (true) {
      echo "True";
} else {
      echo "False";
}
0
 
AlexPonnathAuthor Commented:
That works just fine and returns a true
0
 
Chris StanyonCommented:
Now build on it until it breaks :)

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

echo $txt;
0
 
AlexPonnathAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Chris StanyonCommented:
that's just weird :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now