Jump to content

Please help with an expression


pandu

Recommended Posts

Hello,

 

Check out the following line. Its from a method that generates SEO friendly URLs by querying the DB for the product name. The line is running inside a while..do look that goes thru the entire query result.

 

$define = 'define(\'PRODUCT_NAME_' . $product['id'] . '\', \'' . $this->strip($product['name']) . '\');';

 

First, 'define' is being used as a variable and a function? Err - ins't that disallowed?

Second, what's going on inside those brackets? concatenation, assignment, calling of a method? Holy cow - my brain is going to explode.

 

Please help...

Link to comment
Share on other sites

it's assigning a statement to the $define variable, which i assume is later evaluated as PHP. the statement is just a definition of a constant with the name PRODUCT_NAME_productname and value of whatever strip() does to $product['name'].

Link to comment
Share on other sites

Thanks akitchin - its starting to make sense. Some followup questions:

1. What are those characters before and after PRODUCT_NAME_'.$product['id']. used for?

        \'PRODUCT_NAME_' . $product['id'] . '\'

 

2. Same thing, what are those characters before and after in the following statement:

        \'' . $this->strip($product['name']) . '\'

Link to comment
Share on other sites

those are escaping backslashes. since the string has been delimited by single quotes, any single quotes that you want to put INSIDE of that string need to be escaped, otherwise PHP thinks it's reached the end of the string. for example:

 

$string = 'He said \'I want to go to your house.\' very loudly.';
echo $string;

 

would output:

 

He said 'I want to go to your house.' very loudly.

Link to comment
Share on other sites

Hey thanks a lot for the clarificaiton.

 

But if they are escaping characters, shouldn't the expression be like

\'PRODUCT_NAME_' . $product['id'] .\'

 

instead of

\'PRODUCT_NAME_' . $product['id'] . '\'

 

Similarly, this one

\'' . $this->strip($product['name']) . '\'

 

should be like:

\' . $this->strip($product['name']) . \'

 

Right?

Link to comment
Share on other sites

no - in those cases, the developer INTENDS to exit the string in order to append the variable's value into the string. have a google for a PHP concatenation tutorial, as it should help you clear all this up and will help with future string issues too. it can be tricky, but once you have it down, it's down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.