Solved

php if statement

Posted on 2013-01-15
7
262 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 109

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 109

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

777 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