Saturday, April 28, 2018

Understand PHP Basics – A Beginner Guide


PHP which stands for Hypertext Preprocessor is the leading web development language which is an open source language. You can learn it very easily if you have web designing knowledge. You can learn PHP from best PHP institute in Rohini for attaining advanced methods and techniques. 

This is a beginner guide which consists the important basics of PHP that one should learn before going for advanced level.

Query String in PHP:-

<body>
<h1>PHP Basics</h1>
<a href="doc2.php?subj=PHP&publisher=WileyIndia">Buy PHP Book</a>
</body>

How to Access Query String in PHP:-

<body>
    <h1>PHP Basics</h1>
    <a href="doc2.php?subj=PHP&publisher=WileyIndia">
<h2>Hello! You want <?php echo $_GET['subj']; ?></h2>
<p><em>And Its publisher is: <?php echo $_GET['publisher']; ?></em></p>
</body>

Submit Forms in PHP:-

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Submit Form">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
if ( !empty($name) && !empty($email) ) {
echo "Hello $name<br> Your email is $email";
}else{
echo "empty fields found";
}
}
?>

PHP Array Methods:-

1.)  array():- This method creates an array:

For Example:

<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

2.)  array_pop():- This method deleted the last element of an array:

For Example:

<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>

3.)  array_push():- You can insert multiple elements to the end of array with the help of this method:

For Example:

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

4.)  array_replace():- Through this method you can do the replacement of the first array values with the from following array:

For Example:

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_replace($a1,$a2));
?>

5.)  array_reverse():- This method returns an array in reverse order:

For Example:

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

6.)  array_search():- This method searches an array for a given value and returns the key:

For Example:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

7.)  array_shift():- This method removes the first element of an array and returns the value of removed array:

For Example:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
?>

8.)  array_slice():- This method returns the selected part of an array:

For Example:

<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

9.)  arsort():- This method categories an associative array in downward order, as per the value:

For Example:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
?>

10.)  asort():- This method categories an associative array in rising order, as per the value:

For Example:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
?>

PHP String Methods:-

1.)  chop():- This method removes whitespace or other characters from the right end of a string:

For Example:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>

2.)  implode():- String of elements of an array can be returned through this method:

For Example:

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

3.)  explode():- This method breaks a string into an array:

For Example:

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

4.)  ucfirst():- To convert the string’s first character to uppercase you can use this method:

For Example:

<?php
echo ucfirst("hello world!");
?>

5.)  lcfirst():- To convert the string’s first character to lowercase you can use this method:

For Example:

<?php
echo lcfirst("Hello world!");
?>

6.)  str_replace():- This method replaces some characters in a string (case-sensitive):

For Example:

<?php
echo str_replace("world","Peter","Hello world!");
?>

7.)  strcmp():- This method compares two strings (case-sensitive):

For Example:

<?php
echo strcmp("Hello world!","Hello world!");
?>

8.)  strip_tags():- HTML and PHP tags can be stripped from a string by using this method:

For Example:

<?php
echo strip_tags("Hello <b>world!</b>");
?>

9.)  trim():- This method removes whitespace or other characters from both sides of a string:

For Example:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>

10.)  ucwords():- This method is used to convert the first character of individually word into uppercase which present in a string:

For Example:

<?php
echo ucwords("hello world");
?>

So, we have talked about the query strings, how to use it in PHP, and some essential Array and Strings methods in PHP which are some basic concepts that every PHP beginner should know. We have also seen the important steps to submit form in PHP. 

Hope this guide will provide you aid to understand the basics of PHP. If still have any doubt then join our advanced PHP & MySOL course which will certify you and polish your web development skills. To know more about the training of our institute i.e. Web Development Institute, contact us at +91 9911782350 or 9811818122.

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...