sad cow

web design, jQuery plugins, web tools and general stuff about the internet by matt squirrell.
not exactly unique but hopefully better...

RSS Feed

PHP Nowdoc String Enclosing

I always forget how to do this.

Sometimes you want to put complicated things with lots of quotes and double quotes into a string in PHP, say, if you were storing some Javascript. You could do it like this:
$my_javascript = "$('.someclass').click(function() {
alert ('\"someclass\" has been clicked!')
});";

but eventually all those \" and \' would get confusing.

You can define your own quotes (sort of) in PHP, using something like the following:
$my_javascript = <<<'ASPECIFICTHING'
$('.someclass').click(function() {
alert ('"someclass" has been clicked!')
});";
ASPECIFICTHING;


See how that works? 'ASPECIFICTHING' can be anything you like.

And now if I need to know the syntax again, I can just come back here. Excellent!

It's important to note that the closing word you have chosen must be at the beginning of the line, or it won't be recognised.

Here is the PHP manual page.

CSS Shapes
Installing MySQL 5.1 on Debian
:(

I am Matt Squirrell.

And this is the sort of thing I do.

I can design and build rich, feature-filled websites like this one. Ask me nicely and I might even do it with less than 700KB of images.

I started this website because I'm constantly coming up with neat little fixes, chunks of code, plugins or tricks, and then forgetting about them in a week. This is as much for me as it is for you (but it certainly is nice to share bits and pieces with other people).

It will also serve as a central point for all the other things I have done that have found their way to the Internet - from strange MMO God-sims to a 'Choose your own adventure' maker.

You are more than welcome to hire me if you've got your own web project you want to do - if you can think it I can pretty much make it. I'm cheaper than paying someone from the bargain basement to do it and then having to pay someone good to fix all the problems, too!

Anyway, until I think of a better way to sign off, I only hope you found something useful up there above ground.

Click on the cow ;o)

Contact me. If you like.

Valid XHTML 1.1