We Cover #1 to #6 today
- What is PHP
- Environment Setup
- Basic Structure of the Program
- Execution Method
- Variables
- Data types
- Arrays
- Operators
- Branch Processing
- Iteration
- Function
- Method
- Class
- Practice
What is PHP: Hypertext Preprocessor
PHP is a scripting language, that makes it easy to write and execute programs, so it is said to be for beginners.
PHP is a simple language, but you can build large-scale websites and web applications such as SNS and EC sites.
Since PHP is a server-side language, you can manipulate the information in the database.
In addition, PHP has abundant tools (frameworks) that make website creation easier, so you can develop efficiently.
With PHP, you can speedily build websites using WordPress, which accounts for 34% of all websites in the world.
5 Recommended PHP Frameworks
- Laravel: On GitHub, Laravel has received the highest ratings.
- CakePHP is heavily influenced by Ruby on Rails, allowing you to develop web apps faster.
- Symfony is an open source framework for PHP5 that was introduced in 2005 under the MIT license.
- The Laminas Project is a former Zend Framework, and you can rest assured that your technical partners are good companies such as IBM and GOOGLE.
- CodeIgniter uses the MVC model, which makes it easy to code and is suitable for developing lightweight web applications.
Environment Setup
This time I will use windows, visual studio code, and previously downloaded php without upgrading.
PHP Installation and Configuration
Basic Structure of the Program
The three basic structures of a program are “sequential progress“, “conditional branching“, and “iteration “.
Sequential progress is the structure of a program in which processing is performed in order from the top where the program is written.
Conditional branching is a program structure that processes A when a specific condition is met and B when it is not.
<?php echo "This is my First php". "\n" ;?>
<?php echo "This is my Second php". "\n" ;?>
<?php echo "This is my Third php". "\n" ;?>
If you run php php01.php in powershell, it will be displayed in the following order.
Iteration is a program structure that repeats the same process until a fixed number of times or conditions are met.
Execution Method
If you change the code in the following order…
The order has changed as follows. That is, the code is executed from top to bottom.
Variables
A variable is, for example, a “box“.
By having variables, you can put data such as letters and numbers in variables.
Variables can also be retrieved when needed.
Putting data in a variable is called “assignment“, and fetching it is called “reference“.
Variables can be named and are called variable names.
Creating a variable is called “declaring a variable“.
How to declare variables in PHP
<?php
$number = 1;
echo $number;
In php, variable names start with dollar sign. Substitute the integer 1 for the variable number and refer to it with echo.
Variable names cannot start with a number. Also, you cannot use symbols other than underscores.
The dollar mark is used to define variables, so cannot be used for variable.
Reserved words cannot be used in variable names in other programming languages such as Python.
Reserved words are words that have already been assigned a role in a programming language, such as “return,” “class,” “for,” and “while.”
However, in PHP, reserved words can also be variable names.
<?php
$return = 2;
$class = 3;
echo $return."\n";
echo $class."\n";
Able to execute it without any problems as shown below, but avoid using reserved words in the variable name because unexpected errors may occur and the code will be confusing.
Data types
In PHP, you don’t have to specify the data type such as integer, string, and Boolean, when you put data in a variable.
This is because PHP will automatically determine the data type.
Such a programming language is called a dynamically typed language.
Dynamically typed languages include PHP, JavaScript, Python, and Ruby.
On the other hand, a language that specifies a data type when putting data in a variable is called a statically typed language.
Statically typed languages include C, Java, Kotlin, and Go.
Integer
Returns a data type by substituting integer and decimal numbers.
<?php
$num1 = 12345;
$num2 = 1.234;
echo gettype($num1)."\n";
echo gettype($num2)."\n";
You can check the PHP data type using “gettype ()“.
num1 is an integer type “integer”.
num2 should be a decimal point type “float“, but according to the PHP specification, “double” is returned in the case of “float”.
You can use var_dump to return the correct data type and variable contents.
Float (1.234) was returned as below
String
Enclose the string in single or double quotation.
<?php
$word = "Hello, world!";
echo $word."\n";
echo gettype($word)."\n";
When “Hello, world!” is assigned to the variable word, “Hello, world!” And the data type String are displayed.
Boolean
Boolean, a common programming language, returns either True or False. However, in the case of PHP, “1” is assigned when it is True, and nothing is assigned when it is False.
<?php
$apple = 5;
$banana = 3;
$bool01 = ($apple > $banana);
echo $bool01."\n";
echo gettype($bool01)."\n";
Since 5 is assigned to the variable apple and 3 is assigned to the variable banana, “apple> banana” is True and “1” is returned.