Solved

php if statement

Posted on 2013-01-15
7
261 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:adrian_brooks
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:
adrian_brooks earned 250 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 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 108

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now