• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
PHP Basics
Home
PHP

PHP Basics

May 4th, 2025 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

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:

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:

foreach ($names as $name) {

   echo $name . " was here";

}

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

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.

function add($n1,$n2) {

    return $n1 + $n2;

}

echo add(1,2);

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

  • Tags
  • PHP
  • PHP Basics
Facebook Twitter Google+ LinkedIn Pinterest
Next article 10 Programming Habits that every Developer Should Adopt
Previous article Useful Laravel functions and methods, Part-1

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

Useful PHP built-in functions PHP
May 4th, 2025

Useful PHP built-in functions

Useful Laravel functions and methods, Part-1 Framework
May 4th, 2025

Useful Laravel functions and methods, Part-1

Working with dates in PHP and JavaScript JavaScript
May 4th, 2025

Working with dates in PHP and JavaScript

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Introduction to Git for version control
  • 7 JavaScript array methods you must know as a programmer
  • Why Use MongoDB and When to Use It?
  • Useful PHP built-in functions
  • 7 HTML attributes that you must learn today!
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.