PHP VARIABLES, FUNCTIONS AND NUMBERS

Monday, 1st April 2019

Php statements are a combination of variables, functions and numbers.

BRIEF INTRODUCTION

A variable is preceded with $ (dollar sign). It is used to hold the value of a string, number, array, objects, etc. Mostly at the middle of the program.

Example:

<?php

 $worldtok = “This is a website where programming languages are being taught”;

 ?>

The above is a simple example of a variable in php. It example above worldtok is a variable used to hold a long string.

Numbers in the same vein is the digits from 0 to 9.

Example:

<?php

 $mynumber = 1;

 ?>

Numbers need not to be enclosed with a quotation mark.

Functions in php are the set of instruction which can be executed with one call.

Example:

<?php 

Function multiply(){

echo 2*2;

}

Multiply(); // this will print 4

$myfunction_variable = “multiply”;

$myfunction_variable(); // this will also print 4

?>

The above function will print 4 to the screen when it is called.

Php statement is made up of numerous functions that will help you in performing anything with php. In the state that you can’t find the function that will do exactly what you want you can simply create one for the job.

Some of the php functions out of the box are

<?php

Echo(); //this function takes in a string and prints it to the screen

Date();//this function takes in date object and prints the date according to the format you specify.

Example:

Date(y, m, d); // this will print the date according to year, month, day

Strlen(); // outputs the length of a supplied string

Count(); // counts the number of an array

sizeof(); // this also gives the number of items in an array

?>

PHP VARIABLES

The main way to store information in a PHP program is by using a variable.

From the above example, we can deduce the following as applicable to variables.

• All variables in PHP are denoted with a leading dollar sign ($).

• The value of a variable is the value of its most recent assignment.

Php is an interpreted programming language so variables are interpreted and processed one after the order.

Eg

<?php

$king = “The chief of the community”;

$king = “I’m learning to become the king in programming language ”;

Echo $king; // I’m learning to become the king in programming language



//The above example will print the.

I’m learning to become the king in programming language

?>

 

 which is the last assignment of $king

• Variables are assigned with the = operator (equal sign), with the variable on the left-hand side and the expression to be evaluated on the right.

• Variable declaration and assignment go together.

• PHP does a good job of automatically converting types from one to another when necessary.

• PHP has a total of eight data types which we use to construct our variables:

1.     Integers: are whole numbers, without a decimal point, like 12347890.

2.    Doubles: are floating-point numbers, like 0.89032 or 89.73.

3.    Booleans: have only two possible values either true or false.

4.     NULL: is a special type that only has one value: NULL.

5.     Strings: are sequences of characters, like

 ‘the value inside this quotation is a string'.

6.     Arrays: are named and indexed collections of other values.

7.     Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

8.    Resources: are special variables that hold references to resources external to PHP (such as database connections).

CONCATENATION

Php has a very good concatenation method.

 Concatenation in php is the process of joining two or more strings together to form one string with the combination all the joined string.

Php uses the dot notation (.) to join strings together.

PHP NUMBERS

Numbers in php is just the same as other programming languages.

Php have two types of numbers

Integer and double.

Integers are whole numbers from 0 to infinity

Doubles are numbers with decimal points e.g 1.987 is a double.

PHP FUNCTIONS

Functions are blocks of code that are already defined to carry out a specific task. The block of code may contain other functions that run one after the order and at the end, a final result will be received.

A basic example of a user-defined function is the one I gave above in the introductory section.

The power of php comes in when you learn how to use the functions. This is because a function can be defined and it will be used for many times as long as you keep calling it at the needed location in the appropriate way.

DEFINING A FUNCTION

To define a function you must take the following into consideration

        What you want the function to do.

        The name you will give to the function. This is determined by what you want the function to do.

        Is the function already defined in main php programing language?

        The scope of the function.

        The number of arguments you need for the function to perfectly serve its purpose

Example:

 

<?php

function FunctionName($a, $b){

$a = “My name is ”.” ”.$b;

echo $a;

}

FunctionName($anything, ‘Worldtok..COM’); // my name is WORLDTOK.COM 

?>

From the above function

FunctionNmae is the name given to the function.

This name is what will be used to call the function whenever it is needed to do something.

When calling the function the number of arguments supplied during the function declaration should match the number of arguments passed to it.

These Arguments are what the functions see and take it as variable thereby manipulating anything done on it during the declaration to the passed arguments.

This function defined here has a global scope. 

Another function

<?php 

Function myfunction(){

Echo ‘this is my function with no arguments’

}

myfunction(); // this is my function with no arguments.

 

?>

The above example is a function with no arguments passed. This type of function does not take any argument from you before it is rendered. It’s a function with no argument.

That is it for all you need to know about php variables, functions and numbers.

Leave a Reply

RELATED POSTS


Total of 0 Comment