This question already has answers here:
PHP String Length Without strlen()
(3 answers)
Closed 3 years ago.
PHP coding standards say:
... PHP holds the length property of each string, and that it
shouldn't be calculated with strlen(). Write your functions in a such
a way so that they'll take advantage of the length property, both
for efficiency and in order for them to be binary-safe. ...
How can I access this length property? Or do I misunderstand it?
As Ignacio mentioned in another post:
They're talking about the C function, not the PHP function. The C
function will stop counting after the first \0, but PHP strings can
contain \0 elsewhere other than the end.
That document is most likely about how to extend PHP's engine, rather than programming in PHP itself.
These coding standards are for not intended for web sites developpers using PHP, but for the developpers of the PHP engine itself.
PHP is developped using the C language, and the strlen function to avoid is the C one, not the PHP one.
It's not a "property" in the sense that it would be in other languages (like C#).
You cannot do:
myString.Length;
Instead you would need to do:
strlen(myString);
Related
This question already has answers here:
What does ${ } mean in PHP syntax?
(4 answers)
Closed 7 years ago.
In PHP, what is the difference between placing the dollar-sign in front or within the curly brackets:
1. $var = 'Hello World!';
2.
3. echo "${var}";
4. echo "{$var}";
I understand what it is (variable parsing within a string), however there is not a clear explanation between these two differences in the PHP Manual.
The way the variable is parsed on line 3 is supposed to be simple syntax, whilst line 4 is supposed to be complex syntax.
On the PHP Manual however, the syntax used on line 3 is only used as an example for complex syntax which is quite confusing?
Upon a few tests it seems that they both parse variables as complex syntax?
If possible could anyone provide an example of when to use each one?
If there is no difference, then which convention is preferred?
You are talking about Complex syntax. Looking into example shows that the meaning is same for both cases therefore it is up to you to decide which one to use based on your preferences.
This question already has answers here:
Sitemap urls with special characters [closed]
(6 answers)
Closed 9 years ago.
I want to submit my sitemap to Google, but I don't want to mess anything up. I am also having trouble with the URLs to submit; some of them have special characters in them such as the ampersand (&) symbol and parenthesis (). I just want to know what is the correct way to handle them?
I am currently using PHP's urlencode(), which turns them in to %28, %29 and so on which doesn't really look too good and I am scared if I give Google those links and they go on to index them they will index them as
domain.com/blabla%28blabla.html
Rather than
domain.com/blabla&blabla.html
Are you generating the XML by hand? Please consider using something like the PHP DOM classes instead.
You'll actually want to encode ampersands as &, etc., but it's really best to let a library emit well-formed XML for you.
See Generating XML document in PHP (escape characters) for more discussion of this.
urlencode() is the right function to use. You definately don't want ampersands in your URL because they are a special character used to form a URL (for passing GET variables).
This question already has answers here:
Enumerations on PHP
(39 answers)
Closed 2 years ago.
I have lists where strings correspond with values. For example, in a company list I could have Microsoft correspond with the letters MS.
Obviously, this could be transposed as a table in MySQL to make the list extensible but, from curiosity, how would you express this in constant form in PHP ?
P.S: There is the class-with-constants approach (see accepted answer here: PHP and Enumerations) to act as an enumeration but would that really be of any use seeing that enumerations map to integer values ?
How about using define
define("MS","Microsoft");
echo MS;
This would echo Microsoft.
http://php.net/manual/en/function.define.php
Also the link you gave to a possible solution could just as easily be used with strings instead. The only reason it acts as an enum from other languages is because you define the values from 0 to n, instead of doing that just use string instead.
class Companies {
const MS = 'Microsoft';
const IBM = 'International Business Machines';
}
echo Companies::MS;
I think this would work.
i would probably start by looking at multi dimentional arrays if a single company can have many corresponding letters, however the draw back being that they can get difficult to manage, your other option is to define constants
If you have key-value pairs you can put those into an object, so its more practical to use.
check on this link: the arrayToObject() function
Just browsing over the latest release of the PHP coding standards, and something caught my eye:
http://svn.php.net/viewvc/php/php-src/trunk/CODING_STANDARDS?revision=296679&view=markup
Coding standard #4 states that "When writing functions that deal with strings, be sure to remember that PHP holds the length property of each string, and that it shouldn't be calculated with strlen()..."
I've ALWAYS used strlen, and maybe it's just late, but how do you access the built-in length property of a string in PHP?
They're talking about the C function, not the PHP function. The C function will stop counting after the first \0, but PHP strings can contain \0 elsewhere other than the end.
Its been clearly mentioned that they talking about PHP Coding standards not about C function or extension of PHP engines.
======================== PHP Coding Standards========================
This file lists several standards that any programmer, adding or changing
code in PHP, should follow. Since this file was added at a very late
stage of the development of PHP v3.0, the code base does not (yet) fully
follow it, but it's going in that general direction. Since we are now
well into the version 4 releases, many sections have been recoded to use
these rules.
Still I didn't found any relevant information about string length property but I think in future they might release the information if it's related to new version of PHP.
Please post if someone found useful information about this.
To get the length of a string zval (variable in C) use Z_STRLEN(your_zval)
see zend_operators.h at line 398 (PHP 5.4) :
#define Z_STRLEN(zval) (zval).value.str.len
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why PHP variables start with a $ sign symbol?
I have looked at other programming languages and it seems that most of them do not have any symbol to show that something is a variable. Is there some reason why a PHP interpreter needs such a sign, when interpreters/compilers for other languages are capable of figuring out what is a variable without such a symbol?
Does it make it faster for the interpreter? Does it make it easier for engineers to create an interpreter? Is it to make the code easier to read? Or some other reason?
Bonus question: And if there is a good reason to have a symbol connoting a variable, why don't all programming languages have it?
This is the closest question I could find, although the question seems unclear and the answers range from "just because" to "here's why it's a $ and not some other symbol." That thread did not seem to address the actual purpose of the dollar sign.
EDIT: My question must have been horribly articulated, judging from the confusion in the comments. To clarify, my question is not "Why is the symbol in front of a variable a $ as opposed to some other symbol?", a question that was asked and got four good answers in the page I linked to. My question is "Why is there any symbol at all in front of a variable in PHP? What purpose does it serve to have a symbol in front of a variable?"
Having a symbol to denote variables makes string interpolation simple and clear. Shell, Perl and PHP grew out of the need for quick and easy string manipulation, including interpolation, so I imagine using a variable prefix seemed like a good idea.
I.e. in PHP:
$var = 'val';
$strVar = "The var is $var";
Compare to typical string formatting:
var = 'val'
strVal = 'The var is %s' %(var)
I think it's just from it's origins.
unix shell and Perl were examples.
if you watch PHP closer you will see very much in common with shell.
Thus, you'd better address your question there :)
In php you don't have to set the variables prior to using it, in other languages you have to declare variables before using it like var MyVar = 'my value' or defining what kind of content is going to hold.
I'm not sure but I think the purpose of adding a symbol was to not have to declare variables and let apache know that this is a variable.