Solved

php if statement

Posted on 2013-01-15
7
270 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
phpmyadmin memory error 55 94
Echo'd value as selected in dropdown 20 44
PHP and accessing Array Elements 3 34
How do I init a PHP page jusing JQuery? 8 30
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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