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.