Showing posts with label PHP Tutorial. Show all posts
Showing posts with label PHP Tutorial. Show all posts

Saturday, April 23, 2016

The Best Way to Learn PHP

PHP is most popular programming language for web development and there are lot of opportunities in PHP now a days. So here we'll discuss what would be the best way to learn PHP and how can we explore our knowledge in PHP.

Learn the language first, not a framework or a CMS.
Below is a basic outline of what you'll need to know. Use this as a guide to help you find the information you need. Sorry for not posting any actual resources, but you can consider this a basic syllabus.

Basics

Like anything. Walk before you run. There is a LOT to the "basics" of any language, but these four are where you will derive most value out of PHP. Practice these things until you can do them in your sleep.

The syntax (variables, arrays, if/else, loops, objects, interfaces etc)
Doing stuff with $_GET and $_POST variables, and generally understanding how a request & response in PHP works.
How to perform SQL queries with PDO (don't bother learning mysql, as it's deprecated. Also recommend avoiding mysqli, as its a dated procedural way to code)
How to work with sessions & cookies

Security

Key security concepts to understand when building a PHP application. This is NOT a complete list, but it accounts for most of the vulnerability points an application can make. This is after the basics because it's that important - knowing this stuff is simply not optional.


  • Cross Site Request Forgery protection (CSRF)
  • Filtering input to prevent malicious code execution
  • Filtering input and using PDO prepared statements to stop SQL injection
  • Escaping output to mitigate Cross Site Scripting vulnerabilities (XSS)
  • How to hash and store passwords (php 5.5 made this a lot easier)
  • How to safely upload images & files
  • Configuring your environment for maximum error checking


Object Oriented Design

Once you're comfortable with the basics & security, you should start thinking in terms of objects, and read up on/practice the following concepts. Like the above, do these until they're second nature.
S.O.L.I.D. principles - S is the most important.

S – Single-responsiblity principle
O – Open-closed principle
L – Liskov substitution principle
I – Interface segregation principle
D – Dependency Inversion Principle


  • Programming to an interface
  • Favoring composition over inheritance
  • Dependency injection
  • Namespaces
  • Exceptions (not technically part of OO Design, but it's a bit too advanced


Application Architecture


This refers to how you would structure an application from top to bottom.
MVC pattern (and in general, layered architecture)
Basic understanding of what is meant by a domain and Domain Driven Design (this is a big topic, but you should at least understand the basics)
Using an Inversion of Control container for managing dependency injection (start with Pimple, then checkout Laravel's IoC)
Autoloading & using composer to manage external libraries and dependencies
Various design patterns
Unit testing

Frameworks

Knowing all of the above will help you take greater advantage of (and better appreciate) frameworks. It's easy to do a very bad job using a good framework if you don't quite understand the sections above. Frameworks aren't magic bullets - they require prior knowledge to be used effectively.

There are many frameworks to choose from. The most popular full featured frameworks are Laravel and Symfony, but you should dabble in a wide variety of them to get a sense for how they solve common problems.
There are many more nooks and crannies in PHP than what I've outlined above, but that should be enough to get you started. The key to all of the above is practice and tinkering.

If you're really a beginner, W3Schools (http://w3schools.com) will help you master PHP by using step by step tutorials. This is helpful while so many people want to get their feet wet on PHP didn't know where to start.

If you've mastered the basic and want to advanced to the next level, the PHP website itself (http://php.net/manual/en/index.php) will give you everything you need, all in one place.




Wednesday, April 6, 2016

PHP Tutorial for Beginners

PHP is probably the most popular server side scripting language on the web. It is used to enhance web pages. With PHP, you can do things like create username and password login pages, check details from a form, create forums, picture galleries, surveys, and a whole lot more. If you've come across a web page that ends in PHP, then the author has written some programming code to liven up the plain, old HTML.

PHP is a scripting language, like HTML. That means that code does not need to be compiled before it gets used — it gets processed on the fly as necessary.

PHP Originally created by Rasmus Lerdorf in 1994. PHP originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor.

PHP code may be embedded into HTML code, or it can be used in combination with various web template systems, web content management system and web frameworks.

The mascot of the PHP project is the elePHPant, a blue elephant with the PHP logo on its side, designed by Vincent Pontier in 1998.




So PHP can do a lot of things , few of those are listed below:

  • Take info from web-based forms and use it in a thousands ways (store it in a database, create conditional pages depending on what the forms said, set cookies for later, send e-mail)
  • Authenticate and track users
  • Run threaded discussions on your website
  • Serve different pages to people using different browsers or devices
  • serve XML pages.
What you'll to run PHP application

Before we begin, you will need to install a server on your own machine in order to test your PHP scripts locally. You can install Xampp for Windows machines from https://www.apachefriends.org/download.html  In order to have a Localhost machine. If you're using a Mac you can get MAMP from http://www.mamp.info.

Basic Syntax
It's time to write your own first PHP script. The basic rules of PHP are as follows:

Naming Files: In order to get a PHP script working, the file it’s in or the file that it calls to do the heavy lifting must end in .php (earlier versions used the file extensions .php3 and .phtml). Like HTML, your files are saved as plain text.

PHP’s syntax is derived from many languages - predominantly from the C
language, but Perl has also had a significant influence on its syntax. With the
latest object-oriented additions, more Java-like syntax is creeping in as well.
Despite incorporating elements of so many other languages, PHP's syntax
remains simple and easy to understand

There are five types of tags available:

Standard tags are the de-facto opening and closing tags; they are the
best solution for portability and backwards compatibility, because they
are guaranteed to be available and cannot be disabled by changing PHP’s
configuration file.

Standard Tags

[code]<?php .....code ?>[/code]

Echo Tags

[code]<?= $variableToEcho; ?>[/code]

Prior to PHP 5.4, enabling short tags also made available the short-form
<?= $variable; ?> syntax, also known as "echo tags", which allows you
to print the result of an expression directly to the script’s output. With the
release of PHP 5.4, short tags and echo tags were split, and echo tags are now
always enabled.

Short Tags

[code]<?
... code
?>[/code]

Short tags were, for a time, the standard in the PHP world; however, they do
have the major drawback of conflicting with XML processing instructions (e.g.
<?xml) and, therefore, are no longer recommended and have somewhat fallen
by the wayside.

Script Tags

[code]<script language="php">
... code
</script>[/code]

Script tags were introduced so that HTML editors that were able to ignore
JavaScript but were unable to cope with the standard PHP tags could also
ignore PHP code.

ASP Tags

[code]<%
... code

%>[/code]

Now it's time to write an universal program in PHP "Hello, World!" embedded in an HTML document:

[code hl="13"]
<!DOCTYPE html>
<html>
    <head>
        <title>Tech Bowl</title>
    </head>
    <body>
        <?php echo '<p>Hello World</p>'; ?>
    </body>

</html>
[/code]

Comments:

As with tags, PHP gives you multiple choices for your comments:

[code]// Single line comment
# Single line comment
/* Multi-line
comment
*/
/**
* API Documentation Example
*
* @param string $arg
*/
function abc($arg) { }[/code]

Both types of single-line comments, // and #, can be ended using a newline  (\r, \n or \r\n) or by ending the current PHP block using the PHP closing tag: ?>.

Data Types
PHP supports many different data types, but they are generally divided into two categories: scalar and composite.
A scalar data type contains only one value at a time. PHP supports four scalar types:

Data Type                          Description
boolean                        A value that can only be either true or false
int                                A signed numeric integer value
float                             A signed floating-point value
string                           A collection of binary d

Compound Data Types
In addition to the scalar data type that we have just examined, PHP supports
two compound data types—so called because they are essentially containers of
other data:

Arrays are containers of ordered data elements; an array can be used
to store and retrieve any other data type, including numbers, boolean
values, strings, objects and even other arrays.

Objects are containers of both data and code. They form the basis of
Object-oriented Programming, and are also discussed in a separate
chapter called Object Oriented Programming in PHP.

NULL indicates that a variable has no value. A variable is considered to
be NULL if it has been assigned the special value NULL, or if it has not
yet been assigned a value at all - although, in the latter case, PHP may
output a warning if you attempt to use the variable in an expression.

The resource data type is used to indicate external resources that are
not used natively by PHP, but that have meaning in the context of a
special operation - such as, for example, handling files or manipulating
images.