Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php if statement

Posted on 2013-01-15
7
Medium Priority
?
299 Views
Last Modified: 2013-01-27
Why does this not work

if (!$pcuid) {$pcuid = htmlspecialchars($_GET["pcuid"])};

but this seems to

if (!$pcuid) $pcuid = htmlspecialchars($_GET["pcuid"]);

I thought I needed the { }

if not, when do/don't I need them?

The error I get is
Parse error: syntax error, unexpected '}' in /home/USER/sub/registration/index.php on line 11
0
Comment
Question by:tetrauk
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Richard Davis
ID: 38779973
You have a semi-colon after your } bracket.
You need to move it inside the bracket after your code line.
0
 
LVL 12

Accepted Solution

by:
Richard Davis earned 1000 total points
ID: 38779982
And no, you don't need to use brackets although they do help make for easier reading of code.

The only time you truly require brackets is when you want multiple lines of code to execute conditionally.

But if you're doing a single line of code after an if condition, then no brackets are needed unless desired.

So, either do this;

if (!$pcuid) { $pcuid = htmlspecialchars($_GET["pcuid"]); }

or this;

if (!$pcuid) $pcuid = htmlspecialchars($_GET["pcuid"]);
0
 
LVL 1

Author Comment

by:tetrauk
ID: 38780664
Do I not need a ; to finish the if statement?
0
Technology Partners: 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!

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 38781114
Do I not need a ; to finish the if statement?
This statement will work:

if (!$pcuid) $pcuid = htmlspecialchars($_GET["pcuid"]);

... and this statement will work:

if (!$pcuid)
{
    $pcuid = htmlspecialchars($_GET["pcuid"]);
}

I would probably choose the latter.  But this all begs the question, "How does $pcuid get defined?  It almost seems if this code is trying to step away from Register Globals in baby steps!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7317-Register-Globals-a-bad-idea-from-day-one.html

After you read the article if you still have questions, please post back and I'll try to help. ~Ray
0
 
LVL 1

Author Comment

by:tetrauk
ID: 38821994
ok, i just learnt something, i never even knew about register globals, i'm a very novice php programmer trying to make a script.

basically a .bat file on a pc will get info, create variables and then open internet explorer passing the variables into the url. thus the GET commands

after that a form on the page allows editing of the items then posts the data to itself (sql updates its database with new data)

the part you see is part of this statement

# If no session data, dump gets or posts to session vars
if (!$_SESSION['pcuid']) {
$_SESSION['pcuid'] = htmlspecialchars($_GET["pcuid"]);
if (!$_SESSION['pcuid']) {$_SESSION['pcuid'] = htmlspecialchars($_POST["pcuid"]);}
}

which resulst in either post or get data being passed into a session variable.

it works but i doubt its that great coding.....i have about 10 different sets of this in a row to get each variable.


maybe you have a better solution....i can say at least i'm NOT using register globals, however i am being naughty and not defining my variables first but i can sort that.


any advice for a novice?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38822172
A few thoughts.  

First, you would definitely benefit from some structured learning about PHP, and to that end I recommend this book.  Very readable and full of good examples, it comes with its own downloadable code library that you can copy and modify for your own use.  Now in its fifth printing, it's been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql5/

Second, you might want to add error_reporting(E_ALL) to the top of your scripts so that you can be sure that you're not accidentally relying on an undefined variable.  You can test variables with PHP functions isset() and empty() before using them.  There are also tests like ctype_digit() and filter_var() that are useful when dealing with external input.

Third, adopt the perspective that computer programs only do one thing -- they provide the rules to transform data from one form to another form.  So it's all about the data, full stop.  To that end, it is necessary to visualize the data at every turn.  What is in the $_SESSION array?  Use PHP var_dump() to print out the contents of the $_SESSION array.  If you have clarity of vision about what you want to do with the data, the programming almost "writes itself."

Last, learn about problem definition and test-driven development.  It will make your work easier, faster and more accurate.  This article can help.  Even if you're not studying regular expressions, the concept pervades in the most successful software development teams.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Best regards and thanks for using EE, ~Ray
0
 
LVL 1

Author Closing Comment

by:tetrauk
ID: 38824238
adrian_brooks answered the first questions, Ray_Paseur provide invaluable follow up information that will help me in future.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

886 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