Solved

php if statement

Posted on 2013-01-15
7
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 110

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 110

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

628 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