Page 1 of 1

semi colon character in php?

Posted: Sat Jan 06, 2007 9:07 am
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

Re: semi colon character in php?

Posted: Sat Jan 06, 2007 9:30 am
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.

Posted: Sat Jan 06, 2007 9:50 am
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>';

Posted: Sat Jan 06, 2007 10:59 am
by sarris
thanks guys!!!

Posted: Sat Jan 06, 2007 3:53 pm
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.

Posted: Sat Jan 06, 2007 4:53 pm
by RobertGonzalez
Single quotes around your markup attributes is also invalid markup, under stricter DTD's.

Posted: Sat Jan 06, 2007 4:55 pm
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

Posted: Sat Jan 06, 2007 6:42 pm
by Z3RO21
http://us2.php.net/sprintf may be of interest as well :)

Posted: Sun Jan 07, 2007 7:00 am
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>

Posted: Sun Jan 07, 2007 6:51 pm
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.

Posted: Sun Jan 07, 2007 8:11 pm
by feyd
Quoting is required. Single and double are both accepted.

Posted: Sun Jan 07, 2007 8:55 pm
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.

Posted: Sun Jan 07, 2007 9:04 pm
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.