PHP learning points (will be updated at any time)
-PHP can be used by embedding it in HTML. Write PHP instructions in <? Php ~?>, And the <? Php ~?> Part is converted to HTML and displayed.
- In PHP, use a semicolon “;” at the end of a sentence to separate sentences.
- Note that an error will occur if you forget the semicolon.
・ Comments are made from “//” to the end of the line. Information that is not related to operation and is mainly used for memos.
- “Echo” is an instruction to output a character string or the like. When outputting a character string, enclose it in single quotation “'” or double quotation "” “.
- Don’t forget to put a half-width space after echo
-
Define variables by prefixing them with a “$” symbol. You can put various values in variables with “$ variable name = value;”.
-
If you assign a value to a variable again, the contents of the variable will be overwritten by the value you assigned later.
・ Only when the number to be added is 1, it is possible to write in abbreviated form. Writing ++ before a variable adds it before the instruction on that line is executed, whereas writing ++ after a variable adds it after the instruction on that line is executed. The same is true for-for subtracting one.
-
Character strings can be concatenated by using the dot “.” Symbol, and character strings can be concatenated, variables can be concatenated with character strings, and variables can be concatenated with each other.
-
If you use “. =”, You can omit the concatenation of variables and character strings.
-
When enclosing a character string in double quotation, if you enclose the variable inside in {}, that part will be replaced with the value contained in the variable (variable expansion). If a character string is enclosed in single quotes, the variable is not expanded, and even if the variable is enclosed in {}, it is regarded as a character string as it is.
-
If there are many branches by if and else if and it is complicated, you can make the code simple and easy to read by rewriting it with a switch statement. When the (expression) of the switch (expression) matches the value of case, the block is executed. If none of the cases match, the default block is executed.
- Note that the end of case and default is: (colon).
- Specify the break instruction at the end of the case block. The break instruction is an instruction to escape from the current block.
- Note that if there is no break instruction, the case blocks that follow will be executed continuously.
-
The basic syntax of an array is “$ array name = array (value 1, value 2, …);”.
-
When adding a value to the end of the array, use “$ array name [] = value;”. You can also override the array value by specifying an index number that already exists.
-
Associative arrays are used to manage multiple data together like arrays. In an associative array, use “=>” to set the key and value, such as “$ array name = array (‘key name'=>'value 1’, …);”.
- The difference from an array is that you can specify a value such as a character string called a “key” instead of an index number to manage individual elements.