PHP Symbols: A Guide Through the Syntax Jungle
\n\nHave you ever looked at someone else's PHP code and felt like you were reading ancient hieroglyphs? Arrows, colons, question marks — it all flashes before your eyes, and your brain refuses to understand what's going on. Sound familiar? Don't worry, you're not alone. The topic of "what does this symbol mean in PHP" is one of the most popular on Stack Overflow, and today we'll break it all down.
\n\nIn this article, we've compiled a complete PHP symbols reference — from basic operators to tricky constructs of modern syntax. You'll learn how to read any PHP code with ease. Let's go!
\n\n\n\n
1. Arrow -> (Object Access)
\n\nThe most frequent visitor in any PHP project. The arrow is used to access object properties and methods. If you see $obj->prop — it means we are accessing the prop property of the $obj object.
<?php\nclass User {\n public $name = 'Alexey';\n public function greet() {\n return "Hello, {$this->name}!";\n }\n}\n\n$user = new User();\necho $user->name; // Alexey\necho $user->greet(); // Hello, Alexey!\n?>\n\nNote: inside the class we use $this->name — this is the same arrow, but pointing to the current object.
2. Double Colon :: (Scope Resolution Operator)
\n\nTwo colons — this is the scope resolution operator. It is used to access static properties, methods, and class constants. Remember: if you see :: — it means you are accessing something that belongs to the class, not its instance.
<?php\nclass Config {\n const SITE_NAME = 'PythonLib';\n public static $version = '1.0';\n public static function info() {\n return self::SITE_NAME . ' v' . self::$version;\n }\n}\n\necho Config::SITE_NAME; // PythonLib\necho Config::$version; // 1.0\necho Config::info(); // PythonLib v1.0\n?>\n\nHere, self:: is a reference to the current class from within it. And from the outside, we write ClassName::.
3. Null Coalescing Operator (??)
\n\nOne of the most useful operators, introduced in PHP 7. It allows you to elegantly handle cases where a variable may be undefined or equal to null. The expression $a ?? $b returns $a if it exists and is not null, otherwise — $b.
<?php\n$username = $_GET['user'] ?? 'Guest';\n// Instead of the bulky construct:\n// $username = isset($_GET['user']) ? $_GET['user'] : 'Guest';\n\n// Can be chained:\n$name = $firstName ?? $nickname ?? 'Anonymous';\n?>\n\n4. Spaceship Operator <=> (Spaceship)
\n\nThree-way comparison operator. Returns -1, 0, or 1 depending on whether the left value is less than, equal to, or greater than the right value. Ideal for sorting.
\n\n<?php\necho 1 <=> 2; // -1 (1 is less than 2)\necho 2 <=> 2; // 0 (equal)\necho 3 <=> 2; // 1 (3 is greater than 2)\n\n// Example with sorting an array of objects\nusort($users, function($a, $b) {\n return $a->age <=> $b->age;\n});\n?>\n\n5. Ellipsis ... (Spread Operator / Variadic)
\n\nThree dots can mean two things. The first is array unpacking (spread) in PHP 7.4+. The second is denoting a variable number of function arguments (variadic).
\n\n<?php\n// 1. Spread for arrays\n$parts = ['apple', 'pear'];\n$fruits = ['banana', ...$parts, 'orange'];\n// ['banana', 'apple', 'pear', 'orange']\n\n// 2. Variadic arguments\nfunction sum(...$numbers) {\n return array_sum($numbers);\n}\necho sum(1, 2, 3, 4); // 10\n?>\n\n6. Question Mark with Colon ?: (Ternary)
\n\nThe ternary operator — a shortened if-else notation. $a ? $b : $c returns $b if