Jump to content

Awesome idea for a PHP language addition


tibberous

Recommended Posts

I have the best idea ever for a language addition, a new escape character, \s

 

It would look at the first proceeding numeric string, and evaluate to 's' unless it equaled 1.

 

So, lets say you wanted to write:

 

You have $x item(s) in your cart

 

Instead of having to write:

 

echo "You have $x item".($x==1?'':'s')." in your cart";

 

You could write:

 

echo "You have $x item\s in your cart";

 

What do you guys think? I'd like to see it in PHP6.

Link to comment
Share on other sites

Well, I know it's used for regex, but I don't see how that has anything to do with regular strings. The dollar sign also has special meaning in PCRE even though it's also used for variables. The string is parsed before it's passed to functions, so there would be no ambiguity. For instance, these will be parsed differently, but will do the same thing anyways:

preg_replace('#\n#', '', $str);
// vs
preg_replace("#\n#", '', $str);

 

I don't think the suggested feature is a good idea though.

Link to comment
Share on other sites

well that's because \n happens to mean the same thing for both regex and as an escaped char in strings.  \s stands for a space, tab or newline char in regex, which is not even remotely the same thing as what the OP suggests it be used for.

 

and the $ does mean something in regex. That's why you have to do all kinds of escaping with it, depending on what you're intention is. 

Link to comment
Share on other sites

and the $ does mean something in regex. That's why you have to do all kinds of escaping with it, depending on what you're intention is. 

 

Yeah, that's what I'm saying. It wouldn't result in any ambiguity because the parsing rules already dictate what would happen if \s was a valid escape sequence for double quoted strings.

Link to comment
Share on other sites

and the $ does mean something in regex. That's why you have to do all kinds of escaping with it, depending on what you're intention is. 

 

Yeah, that's what I'm saying. It wouldn't result in any ambiguity because the parsing rules already dictate what would happen if \s was a valid escape sequence for double quoted strings.

 

Okay in something like this:

 

$string = "blah blah blah\s";

 

There wouldn't be ambiguity because there's no regex happening.  But if you are doing this:

 

preg_match("~blah blah blah\s~",$string);

 

so...what is that supposed to mean? A space/tab/newline, or a plural 'blah'?  Using \n like that is okay because it happens to mean the same thing in both contexts, but it would not mean the same thing with \s.

Link to comment
Share on other sites

That would depend on how you implemented the suggested plural \s escape sequence. There is not following anything that can be interpreted as an integer, so you could do two things: 1) ignore it and return verbatim, or 2) remove the \s. Because PHP is eagerly evaluated, argument parameters are evaluated before they are passed on to the functions. Therefore, substitutions in a double quoted string would be done before preg_match() even gets to take a look at it. If you wanted the PCRE \s you would have to either 1) use a single quoted string, or 2) escape the escape sequence.

Link to comment
Share on other sites

Well regardless of nomenclature, I think you're putting the cart before the horse.  As pointed out by many people, making a single escape char auto-pluralize something is not exactly a simple task...maybe even bordering on impossible.  There are many rules to follow for pluralizing something, and it may or may not be pluralized the same way, depending on context and grammar of the rest of the sentence (assuming it was even written correctly to begin with)...and then multiply that by however many languages out there.

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.