• PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • PHP Jobs
  • Advertisement
PHP Lift
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Youtube
    • RSS
PHP Basics
Home
PHP

PHP Basics

February 2nd, 2017 Ivan Dimov PHP 0 comments

In this article we will teach you some PHP Basics like Data types, Mathematical operators, Comparison operators, Comments, Conditional Statements, Loops, Global functions and Functions.

PHP Basics

Data types

integers (2), floats (2.33), booleans (true/false), strings (‘hello’)

Mathematical operators

+ addition, as in 5 + 2

– subtraction, as in 5 –  1

* multiplication, as in 5 * 2

/ division, as in 6 / 2

% modulus or the remainder of a division operation, as in 4 % 2

Comparison operators

== returns true if the value in the left side of the operator matches the value in the right side; 5 == 5 would be true, 5 == ‘5’ would also result to true while 4 == 5 would return the boolean false

=== returns true if the value in the left side of the operator is the same as the value of the right side of the operator and if they are both of the same data type. Therefore, 5 === 5 would return true, 5 === ‘5’ would return false, as well as 4 ===  5.

> this operator would return true if the value in the left side is greater than the value in the right side (greater than), 4 > 5 would return false while 5 > 4 would return true. Strings are converted to numbers and then compared.

< the less than operator; opposite way of working of greater than.

Comments

You can use  // to comment out the entire line after that sequence of characters. You can also use

/*

Multi-line comments

Here

*/ to comment out entire lines.

If statement

You can use the if statement to execute a piece of code only if a particular condition is met; as in

PHP
1
2
3
if (5 > 3) {
// some application logic
}

The if conditional would check if the statement in it returns true and execute the code inside it only in such a case.

After an if statement, you can use else to execute code if the if statement is falsy:

PHP
1
2
3
4
5
6
7
8
9
10
11
If (5>3) {
 
// do something if 5 > 3
 
}
 
else {
 
// do something only if 5 is not greater than 3
 
}

Loops

You can use foreach ($array as $property) to loop over each array item, like this:

PHP
1
2
3
foreach ($names as $name) {
   echo $name . " was here";
}

You can use the for loop to iterate a particular number of times, like this:

PHP
1
2
3
for ( $i = 0; $i < 10; $i++) {
   echo $i;
}

We initialize the $i variable in the loop to equal zero, add one to it on each iteration and repeat this process until $i is less than 10.

Global functions

PHP has numerous built-in functions that you can use out of the box. They can help you achieve certain tasks without starting from scratch. Functions are just like helpers to which you pass certain values and get a specific result. One such function is mt_rand. You use mt_rand to generate a random number and use it in the following way:

mt_rand(0,999) // this will generate a random number between 0 and 999.
There are also a variety of functions to help you with string operations such as substr, substr_count, strpos, strlen with arrays such as count, array_shift, array_pop and so on. You can view all built-in functions in PHP’s official documentation, located at: http://php.net/manual/en/

Functions

You can create your own functions which will help you not repeating code when you have to execute a piece of logic multiple times. Here is a simple function which adds two numbers and returns them, then they are displayed.

PHP
1
2
3
4
function add($n1,$n2) {
    return $n1 + $n2;
}
echo add(1,2);

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)

Related

  • Tags
  • PHP
  • PHP Basics
Facebook Twitter Google+ LinkedIn Pinterest
Next article Useful PHP built-in functions
Previous article Examining variable types in PHP

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

8 Best PHP Frameworks Of 2017 For Developers Framework
January 15th, 2018

8 Best PHP Frameworks Of 2017 For Developers

jQuery Fullcalendar Integration with Bootstrap PHP MySQL Bootstrap
May 3rd, 2017

jQuery Fullcalendar Integration with Bootstrap PHP MySQL

Sanitizing input with PHP and JavaScript JavaScript
March 15th, 2017

Sanitizing input with PHP and JavaScript

Like Us
Like Us
Recent Posts
  • 8 Best PHP Frameworks Of 2017 For Developers
  • jQuery Fullcalendar Integration with Bootstrap PHP MySQL
  • Useful JavaScript globals
  • Cool CSS3 features
  • Sanitizing input with PHP and JavaScript
Categories
  • API
  • Bootstrap
  • CSS 3
  • Framework
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • MySQL
  • PHP
  • Social
  • Tips
Weekly Tags
  • PHP
  • PayPal
  • Payment API
  • Guzzle
  • Variables
  • PHP Basics
  • api
  • Programming Habits
  • HTML5
  • PHP framework
  • About
  • Privacy Policy
  • Back to top
© PHPLift 2017. All rights reserved.