Upload Your Resume to get Hired Soon:- Upload Now

Basics Of PHP for Beginners

Register Yourself in 30 seconds to get hired Soon- Register Now

Php stands for Hypertext Pre-processor. PHP is a server-side scripting language, that is used to develop static websites or dynamic websites.

Downloading Xamp

Xampp is a working environment for PHP.

To download Xampp from apache friends.org:- https://www.apachefriends.org/index.html and install it after installation starts Apache and MySQL.

After that. Go to browser and write localhost/ PHPMyAdmin. Here you can manage and create the database.

Download VS Code:-

Download Visual studio code by following link:- https://code.visualstudio.com/download

Open Xampp folder –>htdocs. Create a new folder, right-click and open with code.

Upload Your CV to get Hired Soon:- Upload Now

Creating our first PHP file:-

  • Create a file with name index.php
  • write some html and open with live server, then open view page source.
  • you will se your complete code. but if php is used only the output is desplayed not the code.
<?php
echo "Hello World";
?>
//echo outputs one or more strings

Variables:-

They are the containers for holding the values in it. you can create a variable by including the ‘$’ before declaring it.

$variable1=5;
$variable2=3;
echo $variable1+$variable2;
//you do not have to declare the datatype.

The most important thing for PHP is that Php is a case insensitive language. we will use the “<br>” tag for space.

Operators:-

They are symbols that tell the PHP processor to perform certain actions. let’s see the types of operators in PHP.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increamenting and decrementing operators
  • Logical operators

Arithmetic Operators:-

They perform +, -,*,/, exponentiation and modulus operation

echo "value of var1*var2 is";
echo $variable1*variable2;

it will print 10., the value of variable1*variable2. Similarly, other operations can be used.

Assignment Operators:-

These are used to assign a value from one variable to another. Operators: = ,+=, -=,*=, /=

//for example.
$new var=$ variable1;
echo $new var;
//This will print '5'.
$new var+=1
echo $new var;
//this will print '6'.

Comparison Operators:-

Comparison operators are (! = , < = , > = , < , > , = =, =). These operators are used to compare two values in a boolean fashion.

For example.

echo "The value of 1=4 is";
echo var_dump (1==4);

here in the above code, var_dump holds the type and value of the variable. it will print the value of 1=4 is bool (false). As the condition is not true.

Increment /decrement operators:-

Post increment (i++) :-

After assigning the value to the variable the value is incremented

echo$ variable1++;

It will print ‘5’ and the value will be increased by 1. That is, if we print it now it will print 6. On the other hand post, increament works after the execution of the program. if we increament the value using post increament value will be increament but after execution, if we want to see the increament we will have to execute the code again and at the second time the incremented value will be printed.

Pre Increment operator(++i) :-

Before assigning the value to the variable, the value is incremented by 1

echo $--variable1;

Logical Operators:

And (&&):- It returns true if both operands are true.

$myvar =(true and false);
echo var_dump($myvar);

It prints false as only one is true.

OR( || ):- Print true if anyone is true.

$myvar=(true or false);
echo var_dump ($myvar);
//output:- False

Not( ! ):- True if $a is not true.

Data Types:-

String:- Alphanumeric characters are classified as strings.

$var = "Pikacoder";
echo var_dump ($var);
//Output:-  String (9) Pikacoder

Integer:-

$var = 67;
echo var_dump($var)
//output: int(67)

Float:- Number with the decimal point

$var=67.5;
echo var_dump($var);
//output:- Float(67.5)

Boolean:-

$var =true;
echo var_dump($var);
//output:- true

Constant:-

Whose value can not be changed?. To declare a constant in PHP use define.

Project (US Trip Form):-

Create a form using HTML and CSS. All values that we are taking from the user must be stored in the Database.

Steps:-

Open your XAMPP Control Panel, click on MYSQL Admin. Create a database Named Trip–> Create Table with eight columns.

Add details, like name, Datatype, Length etc. and then click Save.

Click On insert, insert a row. Now copy the code of SQL Query.

Create index.php file:-

We will use an extension of MySQL to make a connection with MySQL Database.

$insert=false;
if(isset($_Post['name'])){

Step 1: Set Connection variables

$server="localhost";
$username="root";
$password= " ";

Step 2: Create a database connection

$can = mysql_connect($server, $username,$passwor);

Step 3: Check for connection success.

if(!$con){
die("Connection failed due to".mysql_connect_error());}

Step 4:- Collect Post Variable.

$name= $_Post['name'];
$age=$_Post["email"];
----------------------
----------------------
----------------------

Similarly, select your column and the post variable.

At End

$sql=pase the Sql Query which you have copied

Execute the Query

if($con->query($sql)==true){$insert=true;)
else{ echo "ERROR:$sql<br>$can->error";}

Close the database connection

$con->close();

The text which we want to occur when the user submits the form

<?php 
if($insert == true){
exho "<p class="submitmsg> Thank you for joing </p>";}
?>

Now all you have to do is include this HTML inside PHP. You can copy the content of index.html and paste it in index.php.

Leave a Comment