
PHP Basics
Variables & constants, types, arrays, control flow, functions, namespaces, error handling
1What is the main difference between a variable and a constant in PHP?
What is the main difference between a variable and a constant in PHP?
답변
A constant is defined once and its value cannot be modified afterwards, unlike a variable whose value can change at any time during script execution. Constants are declared with define() or const and do not require the $ symbol before their name. They are particularly useful for storing configuration values that should never change, such as file paths, base URLs, or API keys.
2How to correctly declare a variable in PHP?
How to correctly declare a variable in PHP?
답변
In PHP, all variables must start with the $ symbol followed by the variable name. The name can contain letters, numbers, and underscores, but must start with a letter or underscore. PHP is case-sensitive for variable names, meaning $name and $Name are two different variables. It is not necessary to declare the type when creating a variable because PHP is a dynamically typed language.
3What is the data type returned by the following expression: 5 + 3.2?
What is the data type returned by the following expression: 5 + 3.2?
답변
When an arithmetic operation involves an integer and a floating-point number, PHP automatically converts the result to a float to preserve decimal precision. This automatic type conversion is called type juggling or type coercion. In this example, 5 is an integer and 3.2 is a float, so the result 8.2 will automatically be a float. This is important behavior to understand to avoid calculation errors in financial or scientific applications.
Which function checks if a variable is defined and not null?
How to declare a class constant in PHP?
+22 면접 질문