PHP ZCE mock test, interview preparation, daily lessons under chalk talk

Thursday, November 15, 2012

jQuery check if an element with id exists

How would you if an element with particular id exists on the page?


$('#element').length == 0; // no element found

javascript OR jquery - get the calling function code

We all know javascript is a messy language. There may be a time when you have to fix some frontend bug, and you are wandering around javascript code to know its flow.

One solution to this problem is using attributes of arguments property. Such as

1. alert(arguments.callee);

It will alert the code of the function from this alert is being called.

2. alert(arguments.callee.caller);

It will alert the code of the function from which function containing alert was called.


Its quite a helpful thing!!! now say thanks :P

Tuesday, November 6, 2012

difference between echo and print in PHP

Question : What is the difference between echo and print in PHP? Why do you use one over the other?


Solution: 

print and echo both are more or less the same. Both are used to display strings. 

They are not actually functions in php. They are language constructs. Benefit of language construct is that you dont have to put parantheses for the argument list.

Differences are
  • print has a return value of 1 so it can be used in expressions whereas echo returns void.
  • echo can take multiple parameters. 
  • echo is slightly faster than print.