What I learned about PHP today

(Update: I have documented proof that I am backslash/forwardslash challenged. Sorry for mixing them up. I think I got them right this time)

A bright young developer who works for my client is building a PHP app and is going to consume data from an ADO.NET Data Service that I built.

He was trying to build a URI by inserting a variable for a query filter and ran into trouble.

The reason is that PHP uses $ to identify variables.

$animal = “cat”

“The $animal ate the mouse”

The service URI depends on $ to apply operators.

“http://mydomain\myservice.svc/Animals?$filter animalname eq ‘cat’”     (fixed the direction of the URI slashes)

PHP was confusing the operator for a variable.

“http://mydomain\myservice.svc/Animals?$filter animalname eq $animal”  (fixed the URI slashes)

I figured there must be a way to escape that similar to how we need to escape < and > to display them in html.

Thanks to a thread on  the www.faqts.com website, I found this blog post: How can I format currency properly in PHP?

and the answer which is to escape the $ sign with “\”

“http://mydomain/myservice.svc/Animals?\$filter animalname eq $animal”  (fixed the direction of the slashes)

This the only code I’ve ever seen or written in PHP. Don’t make any leaps that I’m going to go too much further for now. I have a litany of new things to learn that will certainly come before PHP!

#1 Luc Wuyts on 8.20.2009 at 8:54 PM

Are you sure u used " ??

I'm not 100% sure but i thought if you used a string with double quotes, the variable is inserted into the string.

If you use a string with single quotes thie variable is seen as simple text.

#2 Julie on 8.20.2009 at 9:11 PM

I didn't write the code in the real solution, but pretty sure that he's using double quotes for exactly that reason. We want the variable to be interpreted so that the string result is

“http:\\mydomain\myservice.svc\Animals?/$filter animalname eq cat”

#3 Joacim Andersson on 8.21.2009 at 7:37 AM

Hmmm... I'm pretty sure that PHP escapes things in the same manner as C does, which means you use a \ (backslash). That really means that you need to escape all the backslashes in the URL as well.

"http:\\\\mydomain\\myservice.svc\\Animals?\$filter animalname eq $animal".

@Luc, PHP expands variables when they are inside double quotes but not inside single quotes. So you don't need to escape anything if you use single quotes. So you could also use this syntax:

'http:\\mydomain\myservice.svc\Animals?/$filter animalname eq '."$animal"

You still have to use double quotes around $animal at the end to have it expanded (or you probably don't have to have quotes around it at all since it contains a string). The period between the two strings is the concatination character.

#4 Julie on 8.21.2009 at 8:27 AM

well, all he told me that it worked. :) He's the PHP dev, I'm not. THe only PHP code I've ever typed is in this blog post :) I just happened to find that solution for him and send it his way.

Also, if it makes a difference, he's using curl-lib if that makes any type of difference.

I also just realized that I made the same mistake I freuqently do when I'm typing urls outside of a browser. I put the slashes the wrong direction. give me another 20 years to get that right. I'll fix them now.

#5 iDayDream on 8.21.2009 at 9:44 AM

Hi!

Double quotes will parse the variables, single quotes will not. So, you could either escape it like you did or use single quotes (which may be easier in the long run if the URI is dynamic).