Backslash in PHP -- What Does It Mean

Backslash in PHP -- what does it mean?

It's because they're using PHP namespaces. Namespaces are new as of PHP 5.3.

What does a \ (backslash) do in PHP (5.3+)?

\ (backslash) is the namespace separator in PHP 5.3.

A \ before the beginning of a function represents the Global Namespace.

Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.

What does backslash do as prefix to a function name?

Introduced in PHP 5.3, \ is a namespace separator.

In your example, the outer strlen is MyFramework\MyString\strlen, the inner strlen is the global one.

Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.

Reference: http://www.php.net/manual/en/language.namespaces.global.php

What does mean if (\false) (yes, with backslash) in PHP?

It means it's using the false defined in the global namespace..

After a bit of research it turns out the rest of this answer is nonesense... I could swear you were able to do this in PHP at one point in time.

I think this is get around the situation where

<?php
namespace whywouldyoudothis;

false = true;
?>

I have never ever seen anyone code for this but that's what springs to mind.

php string backslash followed by digit

The backslash within the string $str is escaping the character immediately following it, you can prevent this behaviour by using single quotes, or; you can escape the backslash (wait for it...) by using a backslash.

echo $str = "IMAGES\2016\08\01\NM.jpg";

Result: IMAGES?68\NM.jpg

echo $str = "IMAGES\\2016\\08\\01\\NM.jpg";

Result: IMAGES\2016\08\01\NM.jpg

Aside: You could use str_replace or preg_replace to replace each single backslash with two backslashes.



Related Topics



Leave a reply



Submit