Monday, January 28, 2013

Variables

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' 



<?php
$var 
'Bob';$Var 'Joe';
echo 
"$var, $Var";     

 // outputs "Bob, Joe"$4site 'not yet';    
 // invalid; starts with a number$_4site 'not yet';    
 // valid; starts with an underscore$täyte 'mansikka';  
  // valid; 'ä' is (Extended) ASCII 228.?>



Predefined variables

PHP provides a large number of predefined variables to any script which it runs. 

PHP Superglobals
$GLOBALS
Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables. $GLOBALS has existed since PHP 3.
$_SERVER
Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).
$_GET
Variables provided to the script via URL query string. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).
$_POST
Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
$_COOKIE
Variables provided to the script via HTTP cookies. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).
$_FILES
Variables provided to the script via HTTP post file uploads. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated). See POST method uploads for more information.
$_ENV
Variables provided to the script via the environment. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).
$_REQUEST
Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the PHP variables_order configuration directive. This array has no direct analogue in versions of PHP prior to 4.1.0. See also import_request_variables().

Tuesday, January 22, 2013

Objects

Objects

Object Initialization

To initialize an object, you use the new statement to instantiate the object to a variable.
<?phpclass foo{
    function 
do_foo()
    {
        echo 
"Doing foo.";
    }
}
$bar = new foo;$bar->do_foo();?>

Converting to object

If an object is converted to an object, it is not modified. If a value of any other
 type is converted to an object, a new instance of the stdClass built in class is 
created. If the value was NULL, the new instance will be empty. 
Array converts to an object with properties named by array keys
 and with corresponding values. For any other value, a member
 variable named scalar will contain the value.
<?php
$obj 
= (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'?>

Array

Array
An array is a data structure that stores one or more values in a single value. For experienced programmers it is important to note that PHP's arrays are actually maps (each key is mapped to a value). 

PHP - A Numerically Indexed Array
If this is your first time seeing an array, then you may not quite understand the concept of an array. Imagine that you own a business and you want to store the names of all your employees in a PHP variable. How would you go about this?
It wouldn't make much sense to have to store each name in its own variable. Instead, it would be nice to store all the employee names inside of a single variable. This can be done, and we show you how below.

PHP Code:
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";
In the above example we made use of the key / value structure of an array. The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference. The general form for setting the key of an array equal to a value is:
  • $array[key] = value;
If we wanted to reference the values that we stored into our array, the following PHP code would get the job done.
Note: As you may have noticed from the above code example, an array's keys start from 0 and not 1. This is a very common problem for many new programmers who are used to counting from 1 and lead to "off by 1" errors. This is just something that will take experience before you are fully comfortable with it.

PHP Code:
echo "Two of my employees are "
. $employee_array[0] . " & " . $employee_array[1];
echo "<br />Two more employees of mine are "
. $employee_array[2] . " & " . $employee_array[3];

Display:
Two of my employees are Bob & Sally
Two more employees of mine are Charlie & Clare
PHP arrays are quite useful when used in conjunction with loops, which we will talk about in a later lesson. Above we showed an example of an array that made use of integers for the keys (a numerically indexed array). However, you can also specify a string as the key, which is referred to as an associative array.

PHP - Associative Arrays
In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

PHP Code:
$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;

echo "Bob is being paid - $" . $salaries["Bob"] . "<br />";
echo "Sally is being paid - $" . $salaries["Sally"] . "<br />";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "<br />";
echo "Clare is being paid - $" . $salaries["Clare"];

Display:
Bob is being paid - $2000
Sally is being paid - $4000
Charlie is being paid - $600
Clare is being paid - $0


Strings

Strings

A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode. See utf8_encode() and utf8_decode() for some Unicode support.

 

Syntax

A string literal can be specified in three different ways.
  • single quoted
  • double quoted
  • heredoc syntax

Single quoted

The easiest way to specify a simple string is to enclose it in single quotes (the character ').
To specify a literal single quote, you will need to escape it with a backslash (\), like in many other languages. If a backslash needs to occur before a single quote or at the end of the string, you need to double it. Note that if you try to escape any other character, the backslash will also be printed! So usually there is no need to escape the backslash itself.

 



<?phpecho 'this is a simple string';

echo 
'You can also have embedded newlines in
strings this way as it is
okay to do'
;

 // Outputs: Arnold once said: "I'll be back"
 echo 'Arnold once said: "I\'ll be back"'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\\*.*?'; 
// Outputs: You deleted C:\*.*?
 echo 'You deleted C:\*.*?'; 
// Outputs: This will not expand: \n a newline 
echo 'This will not expand: \n a newline'; 
// Outputs: Variables do not $expand $either 
echo 'Variables do not $expand $either';?>

Double quoted

If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters:

Heredoc

Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

 

Floating point numbers

Floating point numbers

Floating point numbers (AKA "floats", "doubles" or "real numbers") can be specified using any of the following syntaxes:
<?php
$a 
1.234$b 1.2e3$c 7E-10;?>
Formally:
LNUM          [0-9]+
DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})
The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format).

Wednesday, January 16, 2013

Integers

Integers

An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}. 

Syntax

Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +).
If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x.
Note 11-1. Integer literals
<?php
$a
= 1234; // decimal number$a = -123; // a negative number$a = 0123; // octal number (equivalent to 83 decimal)$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)?>
Formally the possible structure for integer literals is:

decimal     : [1-9][0-9]*
            | 0

hexadecimal : 0[xX][0-9a-fA-F]+

octal       : 0[0-7]+

integer     : [+-]?decimal
            | [+-]?hexadecimal
            | [+-]?octal

Booleans

Booleans

This is the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Syntax

To specify a boolean literal, use either the keyword TRUE or FALSE. Both are case-insensitive.
<?php
$foo
= True; // assign the value TRUE to $foo?>
 


<?php// == is an operator which test
// equality and returns a boolean
if ($action == "show_version") {
echo
"The version is 1.23";
}
// this is not necessary...if ($show_separators == TRUE) {
echo
"<hr>\n";
}
// ...because you can simply typeif ($show_separators) {
echo
"<hr>\n";
}
?>

PHP Supports Data type


PHP supports eight primitive types.
Four scalar types:
  • boolean
  • integer
  • float (floating-point number, aka 'double')
  • string
Two compound types:
  • array
  • object
And finally two special types:
  • resource
  • NULL
This manual also introduces some pseudo-types for readability reasons:
  • mixed
  • number
  • callback 


Tuesday, January 15, 2013

Escaping from HTML


When PHP parses a file, it looks for opening and closing tags, which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows php to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. Most of the time you will see php embedded in HTML documents, as in this example.

<p>This is going to be ignored.</p>
<?php echo 'While this is going to be parsed.'

 ?><p>This will also be ignored.</p>
You can also use more advanced structures:
????? 10-1. Advanced escaping
<?phpif ($expression) {
    
?>    <strong>This is true.</strong>
    <?php } else {
    
?>    <strong>This is false.</strong>
    <?php }?>
This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print(). There are four different pairs of opening and closing tags which can be used in php. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards. 

10-2. PHP Opening and Closing Tags
<?php echo 'if you want to serve XHTML or XML documents, do like this'?>
<script language="php">
        echo 'some editors (like FrontPage) don\'t
              like processing instructions';
    </script> 
Note:
<? echo 'this is the simplest, an SGML processing instruction'; ?>
    <?= expression ?> This is a shortcut for "<? echo expression ?>"
<% echo 'You may optionally use ASP-style tags'; %>
    <%= $variable; # This is a shortcut for "<% echo . . ." %>  
-----------------------------------------------------------------------
While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two.
Short tags (example three) are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if php was configured with the --enable-short-tags option.
Note: If you are using PHP 3 you may also enable short tags via the short_tags() function. This is only available in PHP 3!
ASP style tags (example four) are only available when they are enabled via the asp_tags php.ini configuration file directive.
Note:  Support for ASP tags was added in 3.0.4.
Note:  Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

Your first PHP Enabled Page

divtag Online Web Tutorials
Your first PHP Enabled Page

Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'?> </body>
</html>
  



Use your browser to access the file with your web server's URL, ending with the "/hello.php" file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server's configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>
This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo() statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the ".php" extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.
If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly. Ask your administrator to enable it for you using the Installation chapter of the manual. If you are developing locally, also read the installation chapter to make sure everything is configured properly. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many PHP support options.
The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML file like this anywhere you want. For more details, read the manual section on the basic PHP syntax.
  • A Note on Text Editors: There are many text editors and Integrated Development Environments (IDEs) that you can use to create, edit and manage PHP files. A partial list of these tools is maintained at PHP Editors List. If you wish to recommend an editor, please visit the above page and ask the page maintainer to add the editor to the list. Having an editor with syntax highlighting can be helpful.
  • A Note on Word Processors: Word processors such as StarOffice Writer, Microsoft Word and Abiword are not optimal for editing PHP files. If you wish to use one for this test script, you must ensure that you save the file as plain text or PHP will not be able to read and execute the script.
  • A Note on Windows Notepad: If you are writing your PHP scripts using Windows Notepad, you will need to ensure that your files are saved with the .php extension. (Notepad adds a .txt extension to files automatically unless you take one of the following steps to prevent it.) When you save the file and are prompted to provide a name for the file, place the filename in quotes (i.e. "hello.php"). Alternatively, you can click on the 'Text Documents' drop-down menu in the 'Save' dialog box and change the setting to "All Files". You can then enter your filename without quotes.

Saturday, January 12, 2013

Top 10 Wrong Ideas About PHP

divtag Online Web Tutorials
Top 10 Wrong Ideas About PHP


  1. PHP is not a compiled language (it is interpreted)
  2. PHP cannot do X (access memory, control hardware devices, or some unusual purpose)
  3. PHP cannot do something that can be done in language X
  4. PHP is only for Web development
  5. PHP is controlled by only one company (Zend)
  6. PHP documentation is bad or insufficient
  7. PHP projects are not reusable because they are not Object Oriented
  8. PHP is worse than Ruby On Rails, Python Django, X language Framework
  9. PHP is not good for high performance scalable Web sites or applications
  10. PHP developers are cheaper because they are not qualified
  11. PHP is not a compiled language (it is interpreted)


  1. compiled language is one that needs to convert source code in that language into a sort of machine code before it can be executed.
  2. An interpreted language is one that allows its code to be executed directly from the source text without a compilation step (convert source text into executable machine code).
  3. PHP is not an interpreted language since PHP 4, which was launched in the year 2000.
    1. When a PHP script is executed, first the PHP source code is compiled by the Zend engine into machine code data named Zend opcodes. These opcodes are stored in the RAM. Then those opcodes are executed to actually run the script.
    2. Diagram of the compilation of PHP source code into Zend Engine opcodes
    3. So PHP is really a compiled language just like Java, C# and others. Otherwise it would be rather slow.
  4. Usually the compiled PHP machine code (Zend opcodes) are not saved to files because it is not necessary. But if that is important to you, there are extensions that can output compiled PHP code to files.
  5. By default, if you run the same script again, PHP source code needs to be recompiled into the RAM every time it is executed. However, there several opcode caching extensions that can save the compiled PHP opcodes to shared memory, so next time a PHP script executed for a different Web server request, the original source no longer needs to be recompiled. It is just loaded from shared memory, thus saving a lot of processing time.
  6. The use of an opcode cache extension is thoroughly recommended for performance reasons, especially on busy sites. There are several free opcode caching extensions that you can use.
  7. So, as you may understand by now, the PHP code execution engine is quite sophisticated these days. But if you need something even more sophisticated, there is the HipHop for PHP compiler. This is an Open Source project developed by Facebook to take the PHP performance to the extreme. It compiles PHP source code into C++, which is then compiled into native machine code into a single Web server binary or a standalone executable program. Facebook uses it to run most of their site.
  8. There are also other PHP compiler projects that convert PHP code into Java bytecodes or .NET assemblies. More details can be found on the PHP compiler performance article already mentioned above.

PHP cannot do X (access memory, control hardware devices, or some unusual purpose)

  1. PHP is an extensible language. If you need something that the main PHP distribution does not implement, you can create PHP extensions, usually by writing some C or C++ code. So PHP can do anything that C or C++ can do.
  2. There are tens, if not hundreds of PHP extensions. Many of them are shipped built-in the main PHP distribution. If you need something that is not built-in PHP, you can always check the PECL PHP extension repository. This is the official repository for less popular PHP extensions written in C or C++ code.
  3. If there is no extension to do something that you need, maybe that is because you have an unusual need. Probably you are addressing a new problem or it is something that nobody else has a need for. So, you can always develop a new extension yourself. If you are not capable of developing C or C++ code, you can always hire another developer to do it for you. So you are never stuck without a solution.

PHP cannot do something that can be done in language X

  1. I doubt that there many relevant things that you cannot do in PHP that you can do in other languages. Maybe you can do things in other languages using different programming styles but that does not mean that you cannot develop the same features in PHP, given all the available PHP extensions.
  2. Still, if you find something that can only be done in some other language or you have to rely in existing components written in that language, you can always try to interface with code written in other languages using special PHP extensions available for that purpose.
  3. This is not a very well known fact, probably because it is not something that has great demand, but there PHP extensions that let you execute code in other languages from PHP scripts, like code in: Java, C# (.NET), Python, Perl, Lua, JavaScript using either V8 or SpiderMonkey engines.
  4. Oh, wait, there is no Ruby extension for PHP as you may have noticed. As I said, that is probably because nobody has such an odd need. Still, if you really have that need, maybe you can convert Ruby into Java using JRuby and then you can use the PHP Java extension to run the converted Ruby code. The same goes for other less popular languages.
  5. It is a long shot that may make you wonder if it really makes sense, but at least PHP does not leave you without a solution.

PHP is only for Web development

  1. PHP most common use is indeed for Web application development. Still you can run PHP outside a Web server using the PHP CLI (Command Line Interface) executable. It is a program that can be started from the command line shell for performing all sorts of operations, being Web site related or not.
  2. Even CPU intensive applications are developed in PHP and run outside a Web server using the PHP CLI program, like for instance sending newsletters to many subscribers. The PHPClasses itself sends millions of newsletter messages every month using the PHP CLI program.
  3. You can even create desktop applications to run on Windows, Linux, Mac or any other Unix flavor using the PHP-Gtk extension. You can also develop Windows specific applications or even Windows services using extensions like WinBinder and other PHP Windows specific extensions.
  4. It is not as if there is great demand for developing these kinds of operating system specific applications, but if you need them, you can develop them in PHP too if you want.

PHP is controlled by only one company (Zend)

  1. If you have already read about the PHP history you can realize that PHP was created by Rasmus Lerdorf in 1994. Over time Rasmus was joined by tens or even hundreds of other developers, including Andi Gutmans and Zeev Suraski, the founders of Zend, as well other core developers that work (or at least worked) for Zend.
  2. I think it is only natural that Zend developers will always try to influence PHP development, so it takes directions that are convenient to their business. After all they have invested their whole business in PHP. If PHP development takes a route that makes Zend irrelevant, it would probably kills their business.
  3. Still stating that PHP developement is controlled only by one company seems to be a great exaggeration. The fact is that PHP was developed and continues to be developed by many more people that are not associated with Zend.
  4. There are even core developers that work for Microsoft, like Pierre Alain Joye, a long time PHP core developer that was hired by Microsoft in the recent years to make PHP run well with Windows and other Microsoft products. There are also developers from Oracle, not just to take care of Oracle database extensions, but also MySQL, which is now also an Oracle product since they acquired Sun.
  5. The fact is that those PHP core developers that are connected to Zend, Microsoft, Oracle or other companies, are just a minority, despite their influence. The majority of core developers are not connected with any company.
  6. If you are concerned that those companies can be evil and try to influence the development of PHP to go in the wrong direction, rest assured that all the other core developers that are not tied to any company are also concerned and watching out for any wrong moves. If you tried to watch the discussions in the PHP internals list for a while you probably have already realized that.

PHP documentation is bad or insufficient

  1. If there is only one good thing about PHP, that is certainly the documentation. The documentation is well structured, clear and, given the necessary time to the documentation team, very complete. The user comments that appear in documentation pages only make it even more rich and complete.
  2. I have yet to see any other software project, Open Source or not, that has better documentation than PHP. The fact that it includes user comments qualify the PHP documentation project to be titled: Documentation 2.0 - used enhanced documentation.
  3. The PHP documentation team does an amazing job. Not only they take care of the main documentation in English, but they also have teams dedicated to translate it to tens of other idioms. That is certainly part of the reason why PHP is so popular everywhere in the world. Kudos to everybody that helps in the PHP documentation.
  4. Still the PHP documentation team cannot do miracles. The aspects that PHP covers are so extensive, that it is impossible to know every obscure detail that may make PHP work in a different way that it is expected in certain platforms. There can be bugs in PHP that may cause incorrect behavior, but that is not the fault of the PHP documentation team.
  5. That is why the user comments are so important. They provide additional information that lets other PHP users learn about unexpected behavior that the documentation team could not anticipate.
  6. Additionally, you may often find in documentation user comments sample code to perform certain tasks that PHP users wished that PHP functions could do. That makes PHP documentation with user comments even richer than it would be without the comments. Isn't that amazing?
  7. PHP projects are not reusable because they are not Object Oriented

    1. One of the reasons why PHP got so popular is the due to several killer applications that dominate the Web market. Several of them come to my mind like Wordpress, Drupal, Joomla, etc.. If you want to work as a PHP consultant, chances are that a good part of your clients will want you to integrate their sites with some of these applications.
    2. Nowadays Wordpress is certainly the most popular of the PHP killer applications. Recently, Matt Mullenweg, the creator of the Wordpress project, announced in his State of the Word 2011 speech that Wordpress is present in 14.7% of the top one million Web sites according to the W3 Techs Web survey. That is a lot!
    3. Matt also commented that many developers tweak their Wordpress installations with plug-ins to make it work as a CMS or eventually other types of applications.
    4. Still Wordpress code is mostly written in non-Object Oriented code. It ships with same base classes to implement some common functionality, but the core functionality is written in procedural code.
    5. This makes evident the fact that not being written with Object Oriented code is not necessary to make the project reusable, even for other purposes that are way beyond the original blogging platform purpose.
    6. But wait, do not get me wrong. This is the PHPClasses site. One of the rules that is mandatory to have PHP components accepted for publication in the PHPClasses site, is that the code that implements the described functionality must be written in the form of classes of Object Oriented code, thus the name of the site: PHP Classes.
    7. The reason for this requirement is that classes encapsulate functionality inside a container called a class. If component functions were global, there would be a greater chance of name clashing when combining multiple components of different sources.
    8. For instance, if two components had a function named "print" , how would the applications tell which of the components they want to call the function print? Classes make it easier to encapsulate functions with the same names within different scopes.
    9. But there is a workaround to avoid that problem without resorting to classes. You can just add a prefix to the functions of each component in order to avoid name clashing. For instance, the MySQL extension provides functions with the prefix mysql_.
    10. This is a very old solution used in the core PHP functions since ever, specially because until version 3 there was no Object Oriented support in PHP. But this practice was kept throughout the years even until today.
    11. Admittedly it is not an elegant solution. Component function name clashing (encapsulation) is just one of the benefits of Object Oriented Programming. But nobody can say that it does not work, or that it prevents PHP projects from being reusable.

    PHP is worse than Ruby On Rails, Python Django, X language Framework

    1. PHP comes with many extensions that provide many features but comparing a language with a full stack framework is like comparing pines with apples.
    2. It is fair to compare PHP with Java, C#, Ruby, Python, or "insert the language of the jour here". It is also fair to compare Ruby on Rails, Django, etc.. with a similar PHP framework. As a matter of fact there are so many similar frameworks in PHP, that we are not mentioning anyone in specific here to avoid being unfair to the fans of each of them.
    3. Personally we think that what the developers of any language need is not exactly to use a framework. What developers need is to embrace a methodology of development that makes them productive.
    4. Once you adopt a consist development methodology, everything becomes mechanic and you take less time to produce that same volume of work, as you just need to repeat the same development steps consistently.
    5. You do not really need to use a specific framework to adopt a consistent development method. For instance, we do not use any framework. we just follow the same development methodology that we have been evolving over the years, so nowadays we are quite productive using that methodology.
    6. We follow good development practices like separating concerns into different code components, but we do not need any MVC framework. Actually we feel that MVC is often an inadequate design pattern for use in the development of scalable Web applications.
    7. We usually separate application concerns in components that are can be distributed easily among different machines by the means of service layers. But that may be a subject for a different post.
    8. The main point here is that you do not need to use the framework X to be productive. What matters is that you follow a consistent development methodology that makes your work mechanic and fluent.
    9. The fact is that certain frameworks impose a certain development methodology. Those frameworks are being called "opinionated" because they reflect the opinion of their creators about how development should be.
    10. So, if you invested in studying a methodology that a certain framework imposes and that makes you productive when you implement your Web applications, fine, stick with it.
    11. But please do not come and tell that the framework X, of which you are a big fan, is the best solution, or worse like saying that PHP developers cannot be equally or more productive than you just because they do not use that other language framework that you love so much. That is just your opinion based just in your own experience. Do not underestimate other developers' experience.

    PHP is not good for high performance scalable Web sites or applications.

    1. The way I see it, performance and scalability are not a matter of language, but rather a matter of application architecture.
    2. When it comes to performance, as mentioned above, PHP is a compiled language, so its is speed is nowadays very good for most Web application purposes.
    3. Facebook is certainly the largest site that was developed in PHP. They do not seem to have scalability issues due to their adoption of PHP.
    4. It is true that they developed their own PHP to C++ compiler to make PHP applications run at top speed. It is also true that for CPU intensive applications, the gains of compiling PHP into a lower level language can be significant.
    5. However, the reality is that most Web applications are not CPU intensive. For instance, one of the activities that Web applications spend most time on is accessing databases.
    6. When your application executes a SQL query, most of the time is spent waiting for the database server to execute the query and return the results. Waiting for a query to execute on a database server in PHP or in a lower level language like C++ is going to take practically the same amount of time.
    7. So, now you may wonder, if PHP can be in practice equally as fast as C++ for database based Web applications, why did Facebook people went through such a great effort to develop a PHP to C++ compiler?
    8. The answer lies in the fact that they also changed the architecture. They do not just compile PHP into C++. They take the scripts of whole PHP applications, compile them into single ball of C++ code and generate a single executable that works as a multi-threaded Web server. Notice the emphasis on the multi-threaded word.
    9. Multi-threaded Web servers use a single process to handle many simultaneous HTTP requests. That saves them a lot of RAM because multiple threads share the same memory pool. This means that they end up needing less Web server machines to handle the same load. For a company like Facebook that has thousands of server machines, the gains are significant.
    10. For most other smaller Web sites, the gains are probably not significant enough to go through the effort of compiling PHP into C++.
    11. That does not mean you should not make an effort to educate yourself to learn and to adopt good architecture optimization techniques. Many of those techniques have been covered in this blog in the PHP performance articles category. Go and read them when you can.
    12. Still, most of those techniques are not language specific. You should adopt them regardless whether you develop your applications in PHP or other languages.

    PHP developers are cheaper because they are not qualified

    1. The economy works all in terms of offer and demand. If there is a product that is wanted by many customers and there is not enough quantity of the product on the market for sale, the prices tend to go up. On the other hand, if the product is abundant on the market and the customers are not buying it much, the prices tend to go down.
    2. The same goes for jobs. If there are more companies looking for qualified candidates than those that are available for hire, the offered salaries tend to go up. On the other hand, if there are more candidates than companies willing to hire them, the offered salaries may go down.
    3. The PHP market is huge because the Web is huge. There are many companies willing to hire qualified PHP Web developers. But for simpler jobs, they do not want to pay much because they can find plenty of candidates with sufficient qualifications. Those jobs can be as simple as installing and customizing existing PHP applications.
    4. But for companies that need to hire developers that not only know PHP, but also have other more sophisticated skills like application architecture planning and deployment, developing high scale Web sites, handle security matters properly, search engine optimization, etc., companies tend to pay much better salaries because developers with all those qualifications are scarce.
    5. So, it is not so much a matter a problem of the PHP developers. It is more a matter of the qualifications that are demanded by the types of PHP jobs that require PHP skills.
    Well, the list of misconceptions about PHP does not end here. We just listed a few that we found more important.
    A good professional should know more than just one language to eventually take more advantage of the opportunities that may show up in their careers. So our advice is that whether you love or hate PHP, do not just stick to PHP or that other language that you prefer instead of PHP.