Although php language seems to be dying, it is still used by many companies and is widely popular among the legacy codebases of many large companies. It was the language that give rise to the current web development field. Learning PHP can also help you alot in your carrier. I guess these motivation is enough for you guys to start learning the php. So let's get started.
Let's learn from the fundamentals.
If you have seen my previous articles i follow certain pattern to learning new languages or framework. This pattern is aiding me to successfully learn the languages.
- Give Output and take Input
- String
- Array
- Class and Object
- Methods
- Loops
I always follow this pattern to learn any languages. So without further a do let's get started.
1) Give Output and take Input
The command line used to give output in the php is simple
<?
echo "Hello world";
//or
print_r("Hello World");
?>
And to take the input we enclosed it inside readlines
<?
$name=readline("Enter the name\n");
echo "The name is $name";
?>
2) String and Variable
In PHP to create a string is as simple way as writing variable name starting with $ symbol.
<?
echo strlen("Hello World");
echo str_word_count("Hello world!");
?>
For Iterating in loop inside the string
<?
$name="Sagarkattel";
for($i=0;$i<strlen($name);$i++){
echo "Index of ".$i."="."$name[$i]\n";
}
?>
For Splitting the string in PHP we use built-in explode function.
<?
$name="Sagar kattel";
$parts=explode(" ",$name);
echo $parts[0];
echo "\n";
echo $parts[1];
?>
3) Arrays
In php the way to iterate the array is as simple as any other language. Just you need to put $ symbol ahead of every variable name or array.
$names=["Sagar","Saurabh"];
4) Class and Object
Creating classes in Php is dead simple, it is as same as other language. The only thing that you need to take care is to write instead of this.name you need to write this->name.
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
<?php
class Person{
public $name;
public $age;
function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
function get_name(){
return $this->name;
}
function get_age(){
return $this->age;
}
}
$name=new Person("Sagar Kattel",20);
echo $name->get_name();
?>
5. Method or Function
You can create method or function in php by writing the keyword function ahead of every function name.
<?php
echo "Hello WOrld";
function speak_name($name){
echo "The name of our hero is ".$name;
}
$number1=20;
function multiply($number){
return $number*2;
}
$name="Sagar Kattel";
speak_name($name);
$result=multiply(3);
echo "The Multiply result is ".$result
?>
6. Loops
You can create loops in php as any other programming lanaguages.
<?php
$i=0;
while($i<10){
print_r($i);
$i++;
}
?>
These are the basics that you must know before diving deep into the PHP programming language. Hope you continue your learning journey. Sayonara <3
Top comments (0)