php question

0
457

Question 1: How to add 30 mins to a date?

$date = date("Y-m-d H:i:s");
date("Y/m/d h:i:s", strtotime("+30 minutes", $date));


Question 2: How to get array elements which present in two different array?

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
/**Array ( [a] => green [0] => red )*/

Question 3: How to replace array elements with another another?

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
print_r($input);

/*Array ( [0] => red [1] => green [2] => blue [3] => black [4] => maroon )*/

Question 4: Count number of elements in an array?

$foodArray = array('fruits' => array('orange', 'banana', 'apple'),
              'veggie' => array('carrot', 'collard', 'pea'));

echo count($foodArray); // output 2
// recursive count
echo count($foodArray, COUNT_RECURSIVE); // output 8

Question 5: How to exchanges all keys with their associated values in an array?

$input = array("oranges", "apples","mango", "pears");
$flippedArray = array_flip($input);

print_r($flippedArray);
/*Array ( [oranges] => 0 [apples] => 1 [mango] => 2 [pears] => 3 )*/

What are different visibility of method/property?
There are 3 types of visibility of method & property and are following
Public: Can be accessed from same class method, child class and from outside of class.
Protected : Can be accessed from same class method, child class.
Private: Can be accessed from same class method only.

class TestClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printValue()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new TestClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal error: Cannot access protected property TestClass::$protected in
echo $obj->private; // Fatal error: Cannot access private property TestClass::$private in C:\wamp\www\arun\class\class.php on line 20
$obj->printValue(); // Shows Public, Protected and Private

Question: Difference between array_map, array_walk and array_filter?
array_map: This function applies the callback to the elements of the given arrays.

$arrayData= array_map('strtolower', array('One','Two','three'));
print_r($arrayData); //array ( [0] => one [1] => two [2] => three )

array_walk: This function apply a user supplied function to every member of an array

function my_function(&$item, $key){
  $item=$item.($key+1);
}
$arrayData=array('One','Two','three');
array_walk($arrayData, 'my_function');
print_r($arrayData); //Array ( [0] => One1 [1] => Two2 [2] => three3 )

array_filter: This function filters elements of an array using a callback function.

function odd_number($item){
  if($item%2==0){
    return false;
    }else{
    return true;
    }
}
$arrayData=array(1,2,3,4,5,6);
$arrayData=array_filter($arrayData, 'odd_number');
print_r($arrayData); //Array ( [0] => 1 [2] => 3 [4] => 5 )


http://letsknowit.com/php-questions

 

What’s the difference between these object callings? Non Static: $var = new Object; $var->function(); Static: $var = User::function(); And also inside a class why should I use the static property for functions?

When to use self vs $this?

Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.

example

class Person {      private $name; 
 public function __construct($name)
 {
  $this->name = $name; 
     }
 public function getName() 
{ 
 return $this->name; 
   }
 public function getTitle() { 
   return $this->getName()." the person";  
   } 
 public function sayHello() { 
 echo "Hello, I'm ".$this->getTitle()."<br/>";  
    }  
  public function sayGoodbye() {  
   echo "Goodbye from ".self::getTitle()."<br/>"; 
     }  } 
  class Geek extends Person { 
    public function __construct($name) {
      parent::__construct($name);  
    } 
   public function getTitle() { 
   return $this->getName()." the geek";  
    }  }
    $geekObj = new Geek("Ludwig"); 
 $geekObj->sayHello();
  $geekObj->sayGoodbye();

This will output:

Hello, I’m Ludwig the geek
Goodbye from Ludwig the person

 

 

Differentiate between alert(), prompt() and confirm() methods

Although jQuery is the new craze, traditional javascript knowledge is still required and forms a core part of a web developer interview. One of the common questions is about making sure the interviewee understands the different mechanisms of communicating with the user. The javascript window object uses dialog boxes to interact with the user. The dialog boxes are created with three methods:
• alert()
• prompt()
• confirm()

alert() dialog

The alert() dialog box is used to communicate a message to the user (generally warnings of missed actions). For example, if the email address entered is wrong, you can use the alert() message to warn the user about it. Developers also use alerts() as a quick and dirty way to debug their applications.

// warning
alert("Invalid email address. Please enter again.");
// debugging
alert(currentCounter);

 

 

The alert() method creates a new pop-up window (dialog box) which contains the user message and an
OK button. This is a modal window and all execution is stopped until the user clicks the OK button in the pop-up box.

 

image

 

prompt() dialog

 

The prompt() method asks the user for some small input such as a password, completion of a form input, or personal information, such as nickname or title. The prompt dialog box pops up with a simple text box. After the user enters text into the prompt dialog box, its value is returned (or null in case the user hit cancel).

 

prompt("Please enter your nickname", "nickname");

 

 

The prompt method takes in 2 arguments – the prompt message and a default value. The default value is optional and if provided is filled in the text box and is selected by default.

 

image

 

confirm() dialog

 

The confirm dialog box is used to confirm a user’s answer to a question. This method takes only one argument, the question you will ask the user. A question mark will appear in the box with an OK button and a Cancel button. If the user clicks the OK button, true is returned; if he or she clicks the Cancel button, false is returned. This is also a modal dialog  – the user must agree before the action is completed. You often see this in shopping cart applications just before placing the order or on file sharing sites just before you delete a file. Â

 

if (confirm("Are you sure you want to delete your profile photo?") == true) {
    alert("Deleting photo...");
}
else {
    alert("Glad you decided against deleting the photo!");
}

 

image

To summarize, the different mechanisms that javascript provides you to interact with the user are alert, prompt and confirm. Hopefully with the explanation, code and screenshots, this is clear to you.

What’s the difference between ‘isset’ and ‘!empty’ in php?

ISSET checks the variable to see if it has been set, in other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a ” “, 0, “0”, or FALSE are set, and therefore are TRUE for ISSET.

EMPTY checks to see if a variable is empty. Empty is interpreted as: ” ” (an empty string), 0 (0 as an integer), 0.0 (0 as a float), “0” (0 as a string), NULL, FALSE, array() (an empty array), and “$var;” (a variable declared, but without a value in a class.

Why is the __autoload function used?

In PHP, the __autoload function is used to simplify the job of the programmer by including classes automatically without the programmer having to add a very large number of include statements. An example will help clarify. Suppose we have the following code:

function __autoload($ClassName)
{ 
include($ClassName.".php");
} 
$CustomClassObj = new CustomClass();
$CustomClassObj1 = new CustomClass1();
$CustomClassObj2 = new CustomClass2();

 

Abstact class

this class is not directoly call .it class is call class is extend to other class

Magic Methods

The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic

http://www.techflirt.com/tutorials/oop-in-php/magic-methods-in-php.html

 

Checked unchecked all

$(“#checkAll”).change(function () {
$(“input:checkbox”).prop(‘checked’, $(this).prop(“checked”));
});

You can try following way to check Browser version.

function myFunction() { 
       if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 ) 
      {   alert('Opera'); }
            else if(navigator.userAgent.indexOf("Chrome") != -1 )      {
            alert('Chrome'); 
  }

 

Design Patterns: The Singleton Pattern

As the name “Singleton” suggests, this method allows us to create one and only one object of a class.

http://code.tutsplus.com/tutorials/design-patterns-the-singleton-pattern–cms-23073

Find missing numbers in array

// given array. 3 and 6 are missing.
$arr1 = array(1,4,5,7);

// construct a new array:1,2….max(given array).
$arr2 = range(1,max($arr1));

// use array_diff to get the missing elements
$missing = array_diff($arr2,$arr1); // (3,6)

print_r($missing);

 

what is is_array and in_array?

is_array — Finds whether a variable is an array

$yes = array(‘this’, ‘is’, ‘an array’);

echo is_array($yes) ? ‘Array’ : ‘not an Array’;

in_array — Checks if a value exists in an array

$os = array(“Mac”, “NT”, “Irix”, “Linux”);
if (in_array(“Irix”, $os)) {
echo “Got Irix”;
}

12) What is the meaning of a final class and a final method?

‘final’ is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overrided.

Job Requirement:  PHP Developer

  • MVC Web framework and web services experience
  • Angular JS & Node JS knowledge with experience of 2 months will be an added advantage
  • AJAX/Json, service architectures, and application development
  • Strong database knowledge (preferably MySQL & Oracle)
  • Knowledge of Scrum and Agile development practices
  • Efficient expertise in PHP, Zend, CakePHP, MySQL, JavaScript, CSS, HTML (including HTML 5)
  • Comfortable with object oriented and procedural programming methodologies
  • Strong knowledge database – stored procedures, triggers, indexes, table normalization
  • Ability to maintain source code, and analyze and fix bugs wherever necessary
  • Ability to maintain existing portals / applications, which are developed in open source technologies
  • Should have strong experience in developing sophisticated APIsÂ
  • Should have strong experience in developing sophisticated API’s