pandu Posted June 18, 2009 Share Posted June 18, 2009 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... Quote Link to comment Share on other sites More sharing options...
akitchin Posted June 18, 2009 Share Posted June 18, 2009 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']. Quote Link to comment Share on other sites More sharing options...
pandu Posted June 18, 2009 Author Share Posted June 18, 2009 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']) . '\' Quote Link to comment Share on other sites More sharing options...
akitchin Posted June 18, 2009 Share Posted June 18, 2009 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. Quote Link to comment Share on other sites More sharing options...
pandu Posted June 18, 2009 Author Share Posted June 18, 2009 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? Quote Link to comment Share on other sites More sharing options...
akitchin Posted June 19, 2009 Share Posted June 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.