Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2003, 12:49 AM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using quotes in PHP statements

Hello, all. I'm presently in chapter 8 of the book. Overall, I haven't run into too much difficulty, except I must admit that having to alter many of the examples because of the register_globals=off has been very challenging considering this book is my first taste of PHP. In any event, I'm determined to stick with it and not give into the temptation of turning the setting on, especially given the security implications.

I just got the font cookie program (pages 282-284) working finally. There is one thing I have been struggling with through the last several programs, though, and I have gut feeling there is a better way to do this. It's the quoting. I know you can escape the quotes with \" to force them to appear, but take this snippet of code from my cookie_test.php modification, for example:
Code:
if ($_COOKIE["font"]["type"]) {
     $fontcookie = $_COOKIE["font"]["type"];
     echo "face=$fontcookie  "; }
It seems that there should be a way to get the $_COOKIE variable in the echo statement, but I can't figure out how. I've run into this with other programs. For instance, if I write:
Code:
if ($_COOKIE["font"]["type"]) echo "face=$_COOKIE[\"font\"][\"type\"]  ";
My PHP page won't even come up. I've also tried the single quotes within those array elements but also to no avail. What am I doing wrong?

Thanks for any help.

Scott Henry
 
Old August 31st, 2003, 01:44 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Scott,
A fine challenge! But not one without a solution. What you need is curly syntax. Its very straightforward.

if ($_COOKIE["font"]["type"]) echo "face={$_COOKIE["font"]["type"]} ";

This is talked about in detail in the PHP manual page on strings.
http://www.php.net/manual/en/language.types.string.php

PHP is able to recognize a single layer of nested array, but when you get into multidimensional arrays, that is arrays more than a single layer deep it has no mechanism for recognition beyond the first layer, that is not without the help of curly syntax. Which will tell PHP that the object inside of curly braces is to be treated as a variable.

The statement may be written in a couple of ways.
An alternate way of using curly syntax:
if ($_COOKIE["font"]["type"]) echo "face=${_COOKIE["font"]["type"]} ";

And by using the join operator:
if ($_COOKIE["font"]["type"]) echo "face=".$_COOKIE["font"]["type"]." ";

When you try to escape the quotes, this makes php want to treat the quotes as literal, I'll bet you'll find the string 'Array["font"]["type"]' in your html source code. The reason being, $_COOKIE is an array so it prints that as its value and ["font"]["type"] because what you wrote caused PHP to display the array nesting literally.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 31st, 2003, 01:51 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also to avoid an error of undefined variable under error_reporting E_ALL use this function:

if (isset($_COOKIE["font"]["type"])) echo "face={$_COOKIE["font"]["type"]} ";

Which means if this exists, execute this statement. Its more explicit than simply saying if this exists and does not contain a null value, execute this statement.

isset($variable) - checks for a variable's existence
empty($variable) - checks for null, integer zero, string zero, empty string, or boolean false value

The combination of these two allows for a much more explicit execution of code, which as you progress in your expertise will find much better than the flimsy alternative.

See also:
http://p2p.wrox.com/topic.asp?TOPIC_ID=1709

: )
Rich




:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old September 2nd, 2003, 12:07 AM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rich:

Thanks so much for taking the time to share such comprehensive replies! You've been very helpful. Once I get a bit more comfortable with the fundamentals of PHP, I'm going to dig into the PHP manual a little more. It is a daunting document at over 1,900 pages, and I must confess I have only glanced through it and read small portions up to this point.

In the meantime, thanks again for helping me with this particular issue.

Scott

 
Old September 9th, 2003, 11:57 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hey Scott,
No problem. I just wanted to point out that php.net also has downloadable versions of the manual. Which I've found to be helpful. IMO the windows help file .chm is the best version. The only downside to downloading the documentation is that the user contributed notes are not included and obviously you are also not going to be getting updates made to it but both are easily remedied with periodic updates and visits to the site. Personally I find it helpful to have right there. Its an enormous resource for writing the PHP language.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old September 10th, 2003, 12:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Totally off the original topic:

If you use Dave's Quick Search Deskbar (and in my opinion, you SHOULD), you can search www.php.net's online manual by just typing "php <search string>". This comes in really useful when you know what a function is named but don't know the order of params, the return value, etc.

For this reason, I use the online version of the PHP manual exclusively. I tried using the CHM manual, but it was more of an inconvenience than a help.



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Quotes in textbox amit_p_patel VB How-To 7 May 22nd, 2007 02:29 AM
double quotes MyronCope Classic ASP Basics 2 November 21st, 2006 09:31 AM
displaying single quotes and double quotes ren_123 Classic ASP Databases 2 February 22nd, 2006 02:17 PM
Double Quotes langer123 Classic ASP Basics 2 March 16th, 2005 06:22 PM
Double Quotes and Single Quotes Problem phungleon Classic ASP Basics 7 May 27th, 2004 01:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.