Today we are going to discuss about PHP programming concepts. Most of us like the language PHP. So I thought to write post series for the people who like to learn PHP. We all wanted to create web pages first. So we forget to learn fundamentals. My advice is don’t be hurry. Learn from basic. Then you have proper idea about everything. Just have little patience. When you learn everything you can perform like a master. So I am starting to write post series for learning PHP. These posts are written from very basic level. So anyone can learn them. If you wish to learn PHP language and create amazing web developments please follow all the posts. Today from this I am telling you about an introduction to PHP learning .
Since we are learning a programming languages these post are not very text posts because there is not much to explain. I explained logics when it’s necessary. Otherwise most of post will filled with codes. Let’s get started.
Post Outline |
|
Open and close tags of PHP
PHP has open and close tags. We write PHP codes inside those two tags.
<?php – PHP open tag
?> – PHP close tag
Can we write HTML inside PHP file?
Yes. We can write html inside PHP. Actually PHP mostly used to web development. When developing web pages we use HTML. In creating web pages we create PHP file. When we want to type PHP inside HTML we can use open and close tags and type it. Yet we can’t type HTML inside PHP open and close tags.
How to run PHP code?
For this you need a local server. You can use a xampp or wampp server. First open your xampp control panel. Then for running a php code you should place the code file inside htdocs. Therefore create a folder inside htdocs and paste your php code file. Then open your browser and type localhost/your folder name/file name. As example,
Localhost/myproject/test.php
Comments in PHP
Comments take special place in programming. Yet most of us forget to use comments inside our codes. Yet it is not a good practice. Comments help us to find details about our code. When there are thousands lines it is hard to find exact function’s code segments. We can use comments for that. Therefore place comments inside code is essential. PHP has two types of comments. They are single line and multi-line comments.
# – Single line comment
// – single line comment
/* */ – multi line comment
# This is a comment
// This is a comment
/* This is a multi-line comment in PHP */
How to write PHP variables
There are instant variables, global and super global variables in PHP. We will discuss about them. Instant variable are the variables you create. They won’t visible to outside the PHP script. The instant variable will only visible to the PHP script that you define it. You can define instant variable with starting $ sign. As examples,
$emp_id$student_name
When writing instant variable don’t put space between $ mark and variable name.
PHP end of a statement
This is something very important when talking about introduction to PHP learning. As a PHP developer you should know this. End of a PHP statement is recognize by the semicolon. Therefore when you are coding at the end of the statement don’t forget to type semicolon. As an example,
$emp_id;$student_name;
PHP global variable
You already know about instant variables. Global variables are opposite of instant variables. That means global variables are visible to other PHP scripts also. So you can easily access a global variable in any PHP script. We write global variables with capitals.
Super global variables in PHP
There are set of super global variables in PHP. These are inbuilt global variables in PHP. We can access data by using these super global variables at anywhere in a PHP scripts. We write super global with capitals. Those super global variables are,
- $_GET – consists any variables provided to the script through the GET method.
- $_POST – consists any variables provided to the script through POST method.
- $_COOKIE – consists any variables provided to the script through COOKIE.
- $_FILES – consists any variables provided to the script through file uploads.
- $_SERVER – consists information such as header, file paths and script locations.
- $_REQUEST – consists any variables provided to the script through user input mechanism.
- $_SESSION – consists any variables that are currently registered in the session.
- $_ENV – consists any variables provided to the script as a part of a server environment.
How to use super global variables in PHP
When using super global variables we write brackets and inside brackets we write the variable name. As an example,
If we use GET method to pass a variable from client side to server side. We can retrieve it like this.
$_GET[‘emp_id’];
How to import PHP scripts to another PHP file
When coding sometimes we need another PHP script to execute a PHP script. In that case we can’t write all that script again and again. It is waste of time. Other than doing it we can simply import a PHP script by using a function in PHP. We can use two functions. As Introduction to PHP learning we will learn this too. They are require and include. Other than them if we want to include it once we can use require_once function also. As examples for importing PHP scripts we can take followings.
Require(‘test.php’);Include(mod_emp.php);
Require-once(‘student.php’);
Data types in PHP
PHP is a loosely type language. Which means PHP doesn’t accept any data type when witting variables. PHP is unlike Java. In java when you write a variable you should define the data type. As an example String stName. In PHP you can write just $stName. You don’t need to write the data types like Boolean, string, integer. Yet in PHP there are these data types. But we are not writing them in front of variables when we initiating them. This will make coding easy from one side. The data types in PHP are,
- Boolean
- Integer
- Float/Double
- String
- Object
- Array
- Resource
- Null
How to output data in PHP
We can initialize the variables and assign values to them. Yet how we see them in the web page. How we visually see output in PHP. For that we have to use a print statement. In PHP we can use either print function or echo statement to see the output. In java you know we can use the system.out.println();. This is also similar like that. Let’s see some examples.
Echo(“Hello good morning”);
Print(“Good afternoon”);
Conclusion
So with this I am going to conclude introduction to PHP learning post from here. My next post will be about PHP operations and control statements in PHP. You can read all the post if you like. Please don’t forget to place a comment if you like this post. If you need more about PHP development check out our how to develop PHP project post. We will meet with our next post. Until goodbye all.