Search

Content

php over view1/26/2017
PHP - Hyper Text Pre Processer

php is loosily coupled language.[We dont have to tell data type]

Global Keyword:
   global x; or global['x']

Echo: will accepct multiple argument, No return Value
Print : Single Argument, Returns 1

Php Data Types:

1.String
2.Integer
3.Boolean
4.Float
5.Array
6.Null
6.Resources
7.Object [ $parent =  new parent();]


Php String Functions:
1.srtlen() [No of characters]
2.str_word_count() [returns no of words]
3.strrev() [Reverse the given stirng]
4.strpos() [Return the position of the stirng] ex strpos("hello world", "world") 6
5.str_replace(find,replace,string)

Define a constant
define(name,value,true/false) [true/false=case sensitive or not default false]

Php Arrays 3 Types
1.Index arrays: [ $arr = array(1,2,3,45,)]
2.Associative arrays: [$arr= array(1=>a,2=>b]
3.Multidimensional arrays: [$arr= array(array(1,2),array(3,4)]

 Php array sort Functions
sort() [acending order]
rsort() [deceinding order]

asort() av [acending value]
ksort() ak [acending key]
arsort() dv [desending value]
krsort() dk [descending key]


Php date and time

date("yyyy/mm/dd h:i:s:a")


Php INCLUDE vs REQUIRE

include:  [warning and continue]
require: [error and stop]

FILE upload:
move_uploaded_file(temppath,targetpath)

COOKIE:
setCookie(name,value,time()+86400*30,"/")

SESSION
session_start()
$_session['name'] = "value"
session_unset()
session_destroy()

php super globlas

$_post
$_get
$_request
$_files
$_session
$_cookie
$_server

ISSSET vs empty

isset [value must be something other than null]
empty[ returns true is it is]


MYSQL CONNECT
mysql_connect(host,username,password)
mysql_select_db(database)


PHP MAGIC METHODS(15 magic methods are ther in php)
1.__construct
2.__destruct
3.__set
4.__get
5.__call
6.callStatic
Programmer no need to call the magic methods that is the reason we call them as magic methods.

__construct , __destruct

__construct:
if php 5 cannot find __construct function then it will search for the method with same name and executes.when object of the class has been created.

__construct called when creation of object. and destruct called when deletion of object
class a
{
   function __construct()
   {
       echo "constructer called"
     }
   function __destructer()
 {
    echo "destrucred called."
   }
}

$aobj = new a(); //constructer is called here.

unset($aobj) // destructer is called


__set //used to set value or property which is not acessible form a class
 __get //used to get value or property which is not acessible from a class
 __call //used to call method which is inacessible or unavalibale
__callStatic //used to call static method which is inaceeble or unavilable


__toString//used to print object values directrly


__isSet //this function is triggerd when isset function is applied on uavalibe property class
__unset //this function is excatly opposite to isset called when unset function to on unavaliable propery in class


__sleep //called when used to seriliaze class object
__wakup //called when used to unserilze class object

__invoke //used to


test dome questions and answers
A thesaurus contains words and synonyms for each word. Below is an example of a data structure that defines a thesaurus:
array("buy" => array("purchase"), "big" => array("great", "large"))
Implement the function getSynonyms, which accepts a word as a string and returns all synonyms for that word in JSON format, as in the example below.
For example, the call $thesaurus->getSynonyms("big") should return:
'{"word":"big","synonyms":["great", "large"]}'
while a call with a word that doesn't have synonyms, like $thesaurus->getSynonyms("agelast") should return:
'{"word":"agelast","synonyms":[]}'
 
solution:
 
<?php
class Thesaurus
{
    private $thesaurus;

    function Thesaurus($thesaurus)
    {
        $this->thesaurus = $thesaurus;
    }

    public function getSynonyms($word)
    {
        $ret=array();
        foreach($this->thesaurus as $key=>$value)
        {
   if($key==$word)
   {
    $myjson = array(
    "word"=>$word,
    "synonyms"=>$value
    );
                $ret = array_merge($ret,$myjson);
   }
            
        }
  if(count($ret)>0)
  {
    return json_encode($ret);
  }
  else
  {
  $myjson = array(
    "word"=>$word,
    "synonyms"=>array(),
    );
   return json_encode($myjson);
  }
    }
}

$thesaurus = new Thesaurus(
    array 
        (
            "buy" => array("purchase"),
            "big" => array("great", "large")
        )); 

//echo $thesaurus->getSynonyms("big");
//echo "\n";
echo $thesaurus->getSynonyms("sadf");