Friday, 20 December 2013

Scope Resolution Operator in PHP

The Scope Resolution Operator :

in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
When referencing these items from outside the class definition, use the name of the class.

Example: 


<?php
class MyClass
{
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo MyClass::CONST_VALUE;
?>

Output:

A constant value
A constant value

#Anbu.A

Class and object - basic example

Here i show an example in PHP, how to create a object for a class and how to call a function using object.

Example:

<?php

// create a simple class
class Myclass
{
   function fun_name()
   {
      echo "Hello world";
   }
}

//create an object & call the function
$object = new Myclass;
$object ->  fun_name();

?>

Output:

Hello world

// by Anbu.A

Wednesday, 18 December 2013

How to split a String as array in PHP

explode:

        explode - Split a string by a substring or a string.

* It returns  an array of strings, each of which is a substring formed by    splitting  the given string.

Example:

<?php

  $str = "hello welcome to explode concept";
  $string = explode(" ",$str);

  echo $string[0];      // returns hello
  echo $string[1];      //returns welcome
  echo $string[2];      //returns to
  echo $string[3];      //returns explode
  echo $string[4];      //returns concept

?>

Note: 

Here i splitted where a empty space is available. same we can split based on a char or number.

I hope it  will helps you.


  

Friday, 29 November 2013

Difference between echo and print in PHP

Difference between echo and print in PHP:

There are some differences between echo and print:

  • echo - can output one or more strings
  • print - can only output one string, and returns always 1

    We can use both echo and print statements as echo() , print().
Tip: echo is very faster compared to print as echo does not return any value

Here is an example:

<?php

echo "Hello world";
echo "PHP","is","a scripting","language"; // it prints multiple parameters.

print "Hello world";
print "PHP is a scripting Language";

?>

I welcome any comments. #Anbu