2

I just noticed that I could use an a variable as an argument, like this: $variable = "This's a string."; function('$variable'), and not like this: function('This's a string');. I can see why I can't do the latter, but I don't understand what's happening behind the scenes that meakes the first example work.

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • 3
    How does `function('$variable')` work for you? `$variable` will never be replaced by the string you set before. Instead the string "$variable" will be given to your function. – sascha Jan 03 '12 at 09:18
  • @Sn0opy Thanks. Woodstock says hi... in Birdanese Sign Language. – Wolfpack'08 Jan 05 '12 at 01:40

3 Answers3

5

Have you heard about formal languages? The parser keeps track of the context, and so, it knows what the expected characters are and what not.

In the moment you close the already opened string, you're going back to the context before the opening of the string (that is, in the context of a function call in this case).

The relevant php-internal pieces of codes are:

  • the scanner turns the sequence between ' and ' into an indivisible TOKEN.
  • the parser puts the individual indivisible tokens into a semantic context.

These are the relevant chucks of C code that make it work. They are part of the inner workings of PHP (particularily, the Zend Engine).

PHP does not anticipate anything, it really reads everything char by char and it issues a parsing error as soon as it finds an unexpected TOKEN in a semantic context where it's not allowed to be.

In your case, it reads the token 'This' and the scanner matches a new string. Then it goes on reading s and when it finds a space, it turns the s into a constant. As the constant and the previously found token 'This' together don't form any known reduction (the possible reductions are described in the parser-link I've given you above), the parser issues an error like

Unexpected T_STRING

As you can deduce from this message, it is really referring to what it has found (or what it hopes it has found), so there's really no anticipation of anything.

Your question itself is wrong in the sense that there's no apostroph in the variable (in the variable's identifier). You may have an apostroph in the variable's value. Do not confuse them. A value can stand alone, without a variable:

<?php
'That\'s fine';
42;

(this is a valid PHP code which just loads those values into memory)

Flavius
  • 13,566
  • 13
  • 80
  • 126
1

function('$variable') shouldn't be working correctly

Characters within the " " escape single quotes

Characters within '' do not escape single quotes (they cant escape themselves!).

Using the "" also lets you use variables as part of a string, so:

$pet = 'cat'
$myStory = "the $pet walked down the street"

function($pet) is the way the function should be passed a string

Abe Petrillo
  • 2,368
  • 23
  • 34
0

use it like this

function('This\'s a string');

maxjackie
  • 22,386
  • 6
  • 29
  • 37
  • 1
    I think, this is not what he wanted to read. He even had an error in reasoning. See my comment for this. – sascha Jan 03 '12 at 09:21
  • 1
    He certainly knows how it's right. He is interested in the reasoning, if you read his question carefully ;) – Flavius Jan 03 '12 at 09:45
  • @maxjackie yeah, I noticed that this works, of course. But, when you have a 4000-line+ string, you can either do a bulk replace of the apostrophies, or you can just use double quotes, right? Anyway, I was just wondering why I didn't get an error when I used single quotes and $variable. hehe – Wolfpack'08 Jan 05 '12 at 01:41