semi colon character in php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

semi colon character in php?

Post by sarris »

hey there.super quick question
i want to give a value to a string in php which contains a semi colon.how can i do that?

more specifically

Code: Select all

$display_block .= "<p><div id="map" style="width: 800px; height: 500px" allign = "right"></div></p>";
the semi colon after 800px creates a problem
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: semi colon character in php?

Post by Chris Corbyn »

sarris wrote:hey there.super quick question
i want to give a value to a string in php which contains a semi colon.how can i do that?

more specifically

Code: Select all

$display_block .= "<p><div id="map" style="width: 800px; height: 500px" allign = "right"></div></p>";
the semi colon after 800px creates a problem
Your problem is not the semi-colon, it's the double quotes. If " starts the string and " ends it, do you see a major issue with your code? Hint: Look at the highlighted version in your post ;) Hint 2: \ is the escape character.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Of course, reading the manual on strings would help you understand what is happening. You have two choice here, once is use double quote around your string, which PHP will treat as a string that needs interpretation and scan it for vars (and which will also require escaping of other double quotes in the srring) or use single quotes (to make the string a literal string) and be done with it. Since there is no PHP in the string you are echoing, wrap that puppy in single quotes and make it a literal.

If you have to use double quotes, then do this:

Code: Select all

$display_block .= "<p><div id=\"map\" style=\"width: 800px; height: 500px\" allign = \"right\"></div></p>";
Otherwise, do this:

Code: Select all

$display_block .= '<p><div id="map" style="width: 800px; height: 500px" allign = "right"></div></p>';
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

thanks guys!!!
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Or the following:

Code: Select all

$display_block .= "<p><div id='map' style='width: 800px; height: 500px' align='right'></div></p>";
By the way a <div> inside a <p> is invalid mark-up.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Single quotes around your markup attributes is also invalid markup, under stricter DTD's.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

Code: Select all

$display_block .= '<p><div id="map" style="width: 800px; height: 500px" allign = "right"></div></p>';
that worked for me super fine and i am glad...sometimes things just have to work and bother no more
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

http://us2.php.net/sprintf may be of interest as well :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Everah wrote:Single quotes around your markup attributes is also invalid markup, under stricter DTD's.
Really? Which ones?

According to http://www.w3.org/TR/html4/intro/sgmltut.html
"By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa."

Obviously SGML comes higher up the chain and its specifications certainly cannot be modified by a mere DTD.

The following mark-up validates just fine under the W3C validator:

Code: Select all

<?xml version='1.0' encoding='iso-8859-1'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>

<html xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>

	<head>
	
		<title >Dwarfs</title>
	
	</head>
	
	<body id='my-sexy-body'>
	
		<ul id='dwarf-list'>
		
			<li class='dwarf'>Doc</li>
			<li class='dwarf'>Grumpy</li>
			<li class='dwarf'>Happy</li>
			<li class='dwarf'>Sneezy</li>
			<li class='dwarf'>Bashful</li>
			<li class='dwarf'>Sleepy</li>
			<li class='dwarf'>Dopey</li>
		
		</ul>
		
	</body>

</html>
As does this:

Code: Select all

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
	'http://www.w3.org/TR/html4/strict.dtd'>

<html lang='en'>

	<head>
	
		<meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>
		<title >Dwarfs</title>
	
	</head>
	
	<body id='my-sexy-body'>
	
		<ul id='dwarf-list'>
		
			<li class='dwarf'>Doc</li>
			<li class='dwarf'>Grumpy</li>
			<li class='dwarf'>Happy</li>
			<li class='dwarf'>Sneezy</li>
			<li class='dwarf'>Bashful</li>
			<li class='dwarf'>Sleepy</li>
			<li class='dwarf'>Dopey</li>
		
		</ul>
		
	</body>

</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know, I may be wrong. It might just be that they need to be quoted, not necessarily double-quoted. I know I read it somewhere, but I cannot find that somewhere for the life of me.

As a general rule, I double-quote my attributes all the time, without exception. But that is kinda my way of doing things, and until I find the place where it says it is required, it is only a preference, not a requirement.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Quoting is required. Single and double are both accepted.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Everah wrote:I know I read it somewhere
I believe the enormous bias towards double quoted attributes (almost 100%) has come about due to poor support of single quoted attributes by some early user agents.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I prefer double quotes mostly just because this looks stupid to me...

Code: Select all

<img src='images/some_image.jpg' alt='This looks stupid' />
I have a lot of preferences like that. I choose not to do things simply because they look bad to me... I'm kind of weird like that.
Post Reply