- PHP Syntax
- Basic PHP Syntax
- Example
- My first PHP page
- PHP Case Sensitivity
- Example
- Example
- PHP Exercises
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- 30+ core PHP examples and tutorials for beginner
- PHP Comments
- Single line comment
- Multi-line comment
- Print “Hello world!”
- Add two numbers
- Combine two or more string in PHP
- Calculate the length of string
- Find substring in string
- Count a words from the string
- Replace word from string
- Reverse a string
- Define array in PHP
- Count array in PHP
- Multidimensional array in PHP
- Sort array in PHP
- File operations in PHP
- Open/Close file
- Create a new file and write some data.
- Calculate the size of file.
- Encode/Decode data in json.
- Encode data in Json
- Decode json data
- Get current Date and Time using PHP
- Convert String to Time
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
The default file extension for PHP files is » .php «.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function » echo » to output the text «Hello World!» on a web page:
Example
My first PHP page
Note: PHP statements end with a semicolon ( ; ).
PHP Case Sensitivity
In PHP, keywords (e.g. if , else , while , echo , etc.), classes, functions, and user-defined functions are not case-sensitive.
In the example below, all three echo statements below are equal and legal:
Example
Note: However; all variable names are case-sensitive!
Look at the example below; only the first statement will display the value of the $color variable! This is because $color , $COLOR , and $coLOR are treated as three different variables:
Example
$color = «red»;
echo «My car is » . $color . «
«;
echo «My house is » . $COLOR . «
«;
echo «My boat is » . $coLOR . «
«;
?>
PHP Exercises
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
30+ core PHP examples and tutorials for beginner
In the previous post, we discussed that how to install PHP in windows using XAMPP. Now in this post, We will practice some core PHP examples related to array, if..else conditions, loops and arithmatic operations.
This is a list of 30+ core PHP examples and tutorials for beginner. As a developer, I would suggest you that please complete your basic (core) first and then move to the CMS(Content Management System) or MVC(Model View Controller) frameworks. If your base is clear than you will not face any problem.
Here i have used many core PHP functions. All the functions are linked to its official website, so you will get more clarification about that function. Without spending much time on extra stuff, let’s do practicing.
PHP Comments
Comments are very important to define your code so other developers can understand your code. In PHP, We can write two type comments as shown below.
Single line comment
You can create a single line comment in PHP using following way.
Multi-line comment
You can create a multi-line comment in PHP using following way.
Print “Hello world!”
There are two functions in PHP i.e. echo() and print() will help you to print string in the PHP. Let’s see the example.
Add two numbers
The best thing about PHP is you don’t need to define data type of variable like ‘int’, ‘float’, ‘double’, ‘string’, etc. It will automatically detect the data type of variable and perform the operation. Let’s see in this example. tiktok takipçi satın al
On the above example, Result will be 15.
Combine two or more string in PHP
In the PHP, If you want to connect two or more string, then you can do that by using . (Dot Operator). Check below example
Calculate the length of string
Here in this example, we will use strlen() function to calculate the characters or length of string. See the below example.
Find substring in string
In this example, we will use strpos() function for that. Check below example.
Count a words from the string
To count a words from string we will use PHP’s inbuilt function str_word_count(). See the example here.
Replace word from string
To replace a word from string there is str_replace() function in PHP. We will use it to perform this example. See the example.
Reverse a string
To perform this example, we have inbult PHP function strrev(). We will use it in the below example. See the example.
Define array in PHP
Array is an important part in the PHP programming. Here is a example to show you that how to define array in PHP example. Remember that array will start index from ‘0’.
Access the above array in PHP.
Result:
Array1
a
b
Array2
x
y
Count array in PHP
To count array values in PHP we use count() function of PHP. Let’s see the examples. Count function will result number of all array values.
Multidimensional array in PHP
Multidimensional arrays are important while working in large database applications. Here in this example you will learn how to define multidimensional array. See the below example.
Name | Age | Adult |
---|---|---|
John | 21 | Yes |
Marcony | 25 | Yes |
Marry | 16 | No |
Now, we will define above table data using array in PHP. See the below code carefully
'John', 'age' => 21, 'adult' => 'Yes' ), array( 'name' => 'Marcony', 'age' => 25, 'adult' => 'Yes' ), array( 'name' => 'Marry', 'age' => 16, 'adult' => 'No' ) ); ?>
Now Lets see, how to access the multidimensional array in PHP. See the below code.
"; echo $name[1]['name']."
"; echo $name[2]['name']."
"; ?>
Result:
John
Marcony
Mary
Same way you can access other data of multidimensional array in the PHP.
Sort array in PHP
If you have many results in the application and you want to sort them by A to Z. Then you can do that by sort() function. See the below example.
Result:
Array
(
[0] => Andrew
[1] => John
[2] => Marcony
)
File operations in PHP
File operations (File I/O) are most important part of any programming language. In file operations, you are able to perform create, delete, open, close, edit, etc. See the below examples to make more sense.
Open/Close file
Consider a file named “test.txt” is inside your current root directory. There is below 5 line data inside it.
This is PHP line 1.
This is PHP line 2.
This is PHP line 3.
This is PHP line 4.
This is PHP line 5.
Now we will open this “test.txt” file using fopen PHP function. See below example. Here we use fopen(), feof(), fgets() and fclose() functions of PHP.
Result:
This is PHP line 1.
This is PHP line 2.
This is PHP line 3.
This is PHP line 4.
This is PHP line 5.
Create a new file and write some data.
Let’s create a new file and write some data inside it using PHP function. To performing this example, we will use fwrite() PHP function.
When the above program execute it will automatically generate a new file named “new.txt” and insert two line “This is text1” and “This is test2“.
Calculate the size of file.
You may have question that how to calculate size of file using PHP. Don’t worry about it. PHP provide a inbuilt function named filesize(). Here in this example we will use filesize() function for get a size of our previous file i.e test.txt file. Check below example.
Result:
103 Bytes
Encode/Decode data in json.
Json(JavaScript Object Notation) is most popular data format for storing and exchanging data. It can easily access with javascript. json_encode() and json_decode() functions available for the encode and decode task
Encode data in Json
See the below example. We will use json_encode function to encode the PHP data into json format
'John', 'age' => 21, 'adult' => 'Yes' ); echo json_encode($data); ?>
Decode json data
In this example, we decode the json data using json_decode() function. See the below example.
'John', 'age' => 21, 'adult' => 'Yes' ); $new_data = json_encode($data); echo ""; print_r(json_decode($new_data)); ?>Result:
stdClass Object
(
[name] => John
[age] => 21
[adult] => Yes
)In this example. First we encode array data to json format using json_encode() function. After that we decode the data from json to array using json_decode() function.
Get current Date and Time using PHP
To get current Date and Time we will use date() function of PHP. See the below example.
"; // Current Year echo date('m')."
"; // Current Month echo date('d')."
"; // Current Date echo date('d-m-Y')."
"; // Date : dd-mm-yyyy echo date('d/m/Y')."
"; // Date : dd/mm/yyyy echo date('d/m/y')."
"; // Date : dd/mm/yy echo date('h')."
"; // Current Hour echo date('i')."
"; // Current Miniut echo date('s')."
"; // Current second echo date('a')."
"; // AM or PM echo date('h:i:sa'); // Time : hh:mm:ss AM/PM ?>Convert String to Time
We will convert string to time using strtotime() function. This is an amazing function which will help you to convert all type date-format into date-time format. Don’t understand, See the below example.
"; echo strtotime("3 October 2005") . "
"; echo strtotime("+5 hours") . "
"; echo strtotime("+1 week") . "
"; echo strtotime("+1 week 3 days 7 hours 5 seconds") . "
"; echo strtotime("next Monday") . "
"; echo strtotime("last Sunday"); ?>Compile the above program into your PC and check what result flash. The above example convert string to time is courtesy of www.w3schools.com
I hope that you enjoy the above list of core php examples. If you like it and think that it will helpful for others then please share it. Thanks for being here. Next time i will come up with another interesting article. Thank you!!