PHP interview questions and answers, from Yahoo!
1. Which of the following will not add john to the users array?
1. $users[] = 'john';
2. array_add($users,'john');
3. array_push($users,'john');
4. $users ||= 'john';
2. What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
3. What would the following code print to the browser? Why?
$num = 10;
function multiply(){
$num = $num * 10;
}
multiply();
echo $num;
4. What is the difference between a reference and a regular variable? How do you pass by reference & why would you want to?
5. What functions can you use to add library code to the currently running script?
6. What is the difference between foo() & @foo()?
7. How do you debug a PHP application?
8. What does === do? What’s an example of something that will give true for ‘==’, but not ‘===’?
9. How would you declare a class named “myclass” ? with no methods or properties?
10. How would you create an object, which is an instance of ‘“myclass’??
11. How do you access and set properties of a class from within the class?
12. What is the difference between include & include_once? include & require?
13. What function would you use to redirect the browser to a new page?
1. redir()
2. header()
3. location()
4. redirect()
14. What function can you use to open a file for reading and writing?
1. fget();
2. file_open();
3. fopen();
4. open_file();
15. What’s the difference between mysql_fetch_row() and mysql_fetch_array()?
16. What does the following code do? Explain what’s going on there.
$date='08/26/2003';
print ereg_replace('“([0-9]+)/([0-9]+)/([0-9]+)'‚¬?,\\2/\\1/\\3,$date);
17. Given a line of text $string, how would you write a regular expression to strip all the HTML tags from it?
18. What’s the difference between the way PHP and Perl distinguish between arrays and hashes?
19. How can you get round the stateless nature of HTTP using PHP?
20. What does the GD library do?
21. Name a few ways to output (print) a block of HTML code in PHP?
22. Is PHP better than Perl? – Discuss.
Posted in: Interview Questions | Tags: interview questions and answers interview php yahoo sort ksort web fooChanges in ajax 4.0 Preview 5
Declarative Attribute Changes
It is no longer necessary to include a sys:activate attribute on the body of a page containing declarative markup.
The attributes used in declarative markup have been modified slightly to minimize the number of namespaces used, and to minimize cross browser issues with some html attributes.
1. All attributes that contain an expression (e.g. {{ .. }}) or markup extension (e.g. {binding ..}) must now be prefixed with “sys:”. For example, previously you could do this:
<input type=”text” value=”{{ foo }}” />
You must now do this:
<input type=”text” sys:value=”{{ foo }}” />
Some attributes such as ‘src’ of an image already required this due to various browser issues or side effects. It was confusing when you needed sys and when you didn’t. Also, “top level bindings” naturally require sys as otherwise the expression/binding would be seen as actual content and possibly rendered to the user.
2. The ‘code’ attributes have been moved into ‘sys’.
code:if -> sys:if
code:before -> sys:codebefore
code:after -> sys:codeafter
3. The ‘class’ attributes have been moved into ‘sys’.
class:foo -> sys:class-foo
4. The ‘style’ attributes have been moved into ‘sys’.
style:font-size=”8” -> sys:style-font-size=”8”
Top Level Bindings
Support for "top level" expressions and bindings. Previously these were only supported within the context of a template marked with sys-template. For example, to bind the value of an input to a javascript object, you might do this:
<input type="text" sys:value="{binding bar,source={{foo}} }" />
This binds the value to the ‘bar’ field of a global ‘foo’ object. You can use other bindings to update the value, or directly with Sys.Observer:
Sys.Observer.setValue(foo, “bar”, “newValue”);
Binding the content of an element as text or HTML
You may bind to the content of a node using the new sys:innertext and sys:innerhtml attributes. Choose the appropriate attribute depending on whether or not you want to allow HTML in the value to be interpreted as HTML. For example, if you have a variable named ‘foo’ set to the value “<p>hello</p>”:
<div sys:innertext=”{{ foo }}”></div>
Will result in seeing <p>hello</p> in the browser.
<div sys:innerhtml=”{{ foo }}”></div>
Will result in the <p> tag being interpret as a paragraph tag.
The difference is in how the value is inserted. “sys:innertext” injects a text node with the given value. “sys:innerhtml” sets the innerHTML of the target element. Note that while “innerText” is an Internet Explorer only concept, “sys:innertext” is not an Internet Explorer only attribute. The name is semantically correct, despite it being implemented differently than IE’s native innerText field.
Template Changes
The pseudo variables accessible within template markup have been improved. Previously there was no way to get to the current context, only the parent context ($parentContext). Also, there was no simple way of getting to the current set of data being rendered.
The $parentContext pseudo variable is no longer. Now there is simply the $context variable. From there you can get to all the information available on the Sys.UI.TemplateContext class:
$context.parentContext
$context.data
$context.dataItem
$context.index
… and others. You also still have access to these for convenience:
$dataItem ß the current dataItem (e.g. $context.dataItem)
$index ß index of the current dataItem (e.g. $context.dataIndex)
$element ß The last element to either begin or close
$component ß The last component that was created
The Sys.UI.Template.instantiateIn() method has been changed to accept the data as well as the dataItem for the context that is created.
Before: template.instantiateIn(container, dataItem, dataIndex, insertBeforeNode, parentContext)
Now: template.instantiateIn(container, data, dataItem, dataIndex, insertBeforeNode, parentContext)
DataView Improvements
DataView now has the following events:
rendering
itemRendering
itemRendered
rendered
DataView Dynamic Templates and Placeholders
From each event you have the opportunity to override the parameters the DataView uses to render. The itemTemplate and itemPlaceholder can both be changed dynamically from the rendering and itemRendering events. This allows you to dynamically determine both the default template and placeholder used for all items, without having to change the corresponding DataView properties, as well as dynamically determine them for each individual item. This enables some interesting scenarios where the template used to render each item can be determined based on the data. It also means each item can be rendered into a different location. For example, you might have a Stock Ticker DataView that renders negative stocks with a negativeItemTemplate and positive ones with a positiveItemTemplate. Or you might have a product listing DataView where items of different categories are rendered into separate placeholders, possibly in very distant parts of the page.
Binding Converters and Expandos
Bindings now support the concept of named converters that are set onto the Sys.Binding.converters field. There are none out of the box, but you may now define your own and refer to them by name. For example:
{binding foo,convert=myconverter}
This would look for a converter defined like so:
Sys.Binding.converters.myconverter = function(valueToConvert, binding) {
// convert
}
In addition, you may now set expandos onto the binding object and refer to them from your converter. This allows you to semantically describe what you want in a binding in any custom way you require for any specific binding. It allows you to parameterize a converter function. For example, this example sets a ‘format’ expando on the binding object despite that not being a field or property the binding natively has:
{binding foo,convert=format,format=MM/dd}
Sys.Binding.converters.format = function(value, binding) {
// binding.format === MM/dd
}
UpdatePanel Support
Preview 4 was not compatible with UpdatePanel in ASP.NET 3.5 due to changes to the data format UpdatePanel renders, and the inline script it renders in ASP.NET 4.0. The 4.0 scripts are now designed to work with either 3.5 or 4.0 on the server. To support UpdatePanel with these scripts, you must replace the partial rendering script (MicrosoftAjaxWebForms.js) with the 4.0 version. If you do not use an update panel, you must disable partial rendering.
No UpdatePanel:
<asp:ScriptManager runat=”server” EnablePartialRendering=”false”>
<Scripts>
<asp:ScriptReference Name=”MicrosoftAjax.js” Path=”~/scripts/MicrosoftAjax.js” />
</Scripts>
</asp:ScriptManager>
With UpdatePanel:
<asp:ScriptManager runat=”server”>
<Scripts>
<asp:ScriptReference Name=”MicrosoftAjax.js” Path=”~/scripts/MicrosoftAjax.js” />
<asp:ScriptReference Name=”MicrosoftAjaxWebForms.js” Path=”~/scripts/MicrosoftAjaxWebForms.js” />
</Scripts>
</asp:ScriptManager>
Complex class declaration confusing forms designer?
I have a class declared as:
public class Foo<T> : Panel where T : Control, IFooNode , new()
{
...
}
Could not find type 'FooTestNameSpace.Foo'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
Interestingly, I ALSO get a warning that is similar, but includes the generic type I used in my declaration of the variable. Warning is:
Could not find type 'FooTestNameSpace.Foo<FooTestNameSpace.FooNodeType>'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
The declaration in my simple Form1 class is:
private FooTestNameSpace.Foo myFoo;
(FooNodeType is really just a subclass of Label that has one auxiliary property not yet being used; it does implement IFooNode).
So my question... With this type of set up, how can I get Foo to be displayed on the Forms designer, or at least get it to acknowledge that Foo is legit? Is there any Designer attribute that can handle this? I don't really care if my Foo control appears as an empty box, as long as it appears at all!
Answer:
class ConcreteFooCtl : FooCtl<FooNodeType>
{
}
class FooCtl<T> : Panel where T : Control, IFooNode , new()
{
}
The problem seems to be that the Forms designer can't handle any generic control. The forms designer can't handle FooCtl, but has no problems with ConcreteFooCtl. Not an ideal solution, but I think it's at least workable. Maybe the next version of VS will prompt the user to specify a generic type when adding a generic control to a form... ;)
Posted in: programming problems and solutions | Tags: foo forms designer declaration complex