Category Archives: Core Php

PHP Datatypes


PHP Datatypes:- A datatype simply describes the way how does a data will be handled by a compiler in a programming lanuage.

PHP supports total eight primitive data types: Integer, Floating point number or
Float, String, Booleans, Array, Object, resource and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.


PHP Datatypes | Example

  1. Integers
  2. Strings
  3. Floating Point Numbers
  4. Booleans
  5. Arrays
  6. Objects
  7. NULL
  8. Resources

Let us understand few of them with examples because here we are assuming that you know some programming knowledge before.

PHP Booleans

Boolean datatype is a data type which contains only two values either true or false.

Following is an example for how to write a boolean in php.

<?php
$value=true;
var_dump($value);
?>

Integer

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).

Example // Integer

<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $c;
echo"<br>";
echo $d;
echo "<br>";
echo $e;
?>

Strings

Strings are like sentences. They are formed by a list of characters, which is really an “array of characters”. Strings are very useful when communicating information from the program to the user of the program. They are less useful when storing information for the computer to use.

Below are some examples of creating strings in php language.

Example

<?php
 $str="1x@Y";
 $name="William_Smith";
 echo $str;
 echo "<br>";
 echo $name;
?>

PHP Floating Point Numbers or Doubles

Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers, like demonstrated in the example below.

The range of floating point numbers in PHP is equivalent to the range of the double type in C, Double can range from 1.7E-308 to 1.7E+308. A double may be expressed either as a regular number with a decimal point or in scientific notation.

Example

<?php
$a = 1.234;
var_dump($a);
echo "<br>";
 
$b = 10.2e3;
var_dump($b);
echo "<br>";
 
$c = 4E-10;
var_dump($c);
?>

PHP NULL

PHP null is a special datatype which contains no value.In following example we can see how does it changes value of any variable.

Example // file_get_contents() Method

<?php
$number=10;
echo $number;
echo "<br>";
$number=null;
var_dump ($number);
?>

Resources

Resource identifiers are acting as a reference or identifier to access resource data provided by a third party external application. These resource data will be return while performing the operations like connecting the database or opening an external file. When we connect the database with the required configuration details like,

$conn = mysqli_connect(localhost,”root”,”admin”,”animals”);
The function will return a resource type data to be stored into $conn variable.

With reference to the resource created, we can get required list of data items from the database.Later we will use this $conn resource variable for many operations on database like for closing a connection for example:- mysqli_close($conn);

Example // file_get_contents() Method

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

PHP Constant


PHP Constant :– A PHP constant is a value which can not be changed during the program execution.An many programming languages,the only difference comes when we declare it in php.

So many people get confused in variable and a constant programatically when we use these two terms in php. So only the difference is a variable name might get changed but a constant can not during the script execution.


PHP Constant | Decleration

To declare and initialising a constant following syntax works for php==>>>define(constant_name, value, case_sensitive=true);

Example


PHP Variable


PHP Variable:– Like other programming languages Java,Perl,.Net all variables are used to store the data so same in php also variables are used to save data for processing in program for generating outputs.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Only the differences comes when we use them in different languages. So we will here how to declare and initialise a variable in php language that is very easy .

Rules for naming convensions for a variable name:

1.A variable should start with $ symbol followed by any letter from a-z or underscore only.
2.No numeric values are allowed after $ symbol.
3.A variable name never contains space.
4.The variable name is case-sensitive.


PHP Variable | Description

A PHP variable name is started with $ symbol followed by a name.
For example $x=10; is a declaration and initialisation for integer type variable.Similarily we can define a variable name for different data types like string ,boolean ,float etc.
Rest of declaration you can do easily as here we assume that you know programming a little bit earlier.
Syntax for Variable declaration: $varname=variable type;

Here is a simple example how to define a variable in php. Beginners can see below screeshot for invalid variable name generated by localhost on output console screen.

Example


PHP Syntax


PHP Syntax :– PHP syntax is easy to learn. Syntax rules tells us how to write statements, varaiable and functions etc in any programming language. Most commanly used starting and ending tags are respectively. So now here we are going to discuss more specific example for php syntax but before going ahead one thing is to remember is the extension for php file is .php simply means all php files would be ended with .php keyword while saving in any editor.


PHP Syntax | Example

A PHP script syntax is much similar to html unlike some starting and ending tag described as follows:

You can see the following example how php syntax looks inside html code:

Example

<?php
echo "hello php";
?>


PHP Installation


PHP Installation :– Here is the right way for absolute beginner to learn php. Let we tell you how to install and work with php.

Firstly what we need is to install xampp package in your windows pc. To download xampp simply click the following link https://www.apachefriends.org/xampp-files/5.6.30/xampp-win32-5.6.30-0-VC11-installer.exe.

XAMPP is a free and open source cross-platform web server developed by Apache Friends. XAMPP stands for Cross-Platform (X), Apache (A), MySQL (M), PHP (P) and Perl (P).

Now you can test Apache server whether it is working or not on your machine. You can type http://localhost/ in your url to test apache server configuration whether it is working or not.Below screenshot shows that you have succesfully installed apache server on your local machine.


PHP Installation | Guide

Here are the following steps to install PHP on local environment.

To Make sure your web server really works with php script file; go into C:/xampp/htdocs and create a php file in php editor like notepad++ and save it at c:\xampp\htdocs\xyz.php then open your browser and type http://localhost/index.php and hit enter now you will get output of php file.

Let us create first example in PHP:-

PHP Hello World Program Example:


PHP Tutorial Hello world example

PHP Introduction


PHP Introduction :– PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Development Team. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.

PHP code may be embedded into HTML or HTML5 markup, or it can be used in combination with various web template systems, web content management systems and web frameworks. PHP code is usually processed by a PHP interpreter implemented as a module in the web server or as a Common Gateway Interface (CGI) executable. The web server software combines the results of the interpreted and executed PHP code, which may be any type of data, including images, with the generated web page. PHP code may also be executed with a command-line interface (CLI) and can be used to implement standalone graphical applications.

PHP is server side scripting language which runs only on php enabled server like Apache web server or Apache Tomcat, It is used to create dynamic web pages. PHP is open source so you can use it fre of cost. Here in this tutorial we are going to explain the core php basic and advance concept. We will explain each topic with multiple examples.

Devised in 1994 by programmer Rasmus Lerdorf, PHP goes beyond and supplements HTML in several ways. The script can perform complex calculations such as providing date ranges through to figuring out mathematical equations; it is adept at collecting user information such as address data; and crucially PHP interacts with databases, allowing for the creation of web pages that utilise database contents to create page content.

Perhaps the most notable example of the use of PHP comes in the shape of Facebook. Mark Zuckerberg and his programming peers were huge fans of PHP’s flexibility, and the website still uses this scripting foundation today.


PHP Simple Program | Example

The current stable version of PHP is 7.1.4. We will also cover PHP 7.1.4 changes and concepts in this tutorial. PHP is very easy and simple to Learn.

Let us create first example in PHP-

PHP Hello World Program Example:


If you run the above example it will produce output something like this-