0% found this document useful (0 votes)
2 views

php and javascript

JavaScript is a client-side scripting language used for dynamic web pages, developed by Brendan Eich in 1995. It supports various data types, operators, and variable scopes, allowing for interactive user experiences. PHP, created in 1994, is a server-side language for web development, known for its ease of use and extensive database support.

Uploaded by

kingaayush755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

php and javascript

JavaScript is a client-side scripting language used for dynamic web pages, developed by Brendan Eich in 1995. It supports various data types, operators, and variable scopes, allowing for interactive user experiences. PHP, created in 1994, is a server-side language for web development, known for its ease of use and extensive database support.

Uploaded by

kingaayush755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JAVASCRIPT

Definition
JavaScript is a scripting language which is done on a client-side. The various browser supports
JavaScript technology. DHTML uses the JavaScript technology for accessing, controlling, and
manipulating the HTML elements. The statements in JavaScript are the commands which tell the
browser for performing an action.
JavaScript is a dynamic computer programming language. It is lightweight and most commonly
used as a part of web pages, whose implementations allow client-side script to interact with the
user and make dynamic pages. It is an interpreted programming language with object-oriented
capabilities.

History
Javascript was invented by Brendan Eich in 1995. It was developed for netscape 2, and became
the ECMA-262 standard in 1997. After netscape handed javascript over to ECMA, the mozilla
foundation continued to develop javascript for the firefox browser. Mzilla’s latest version
1.8.5.(Identical to ES5).

Javascript variables
There are some rules while declaring a JavaScript variable (also known as identifiers).
 Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
 After first letter we can use digits (0 to 9), for example value1.
 JavaScript variables are case sensitive, for example x and X are different variables.
Correct JavaScript variables
var x = 10;
var _value="
sonoo";
Incorrect JavaScript variables
var 123=30;
var *aa=320;
Example:-
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Javascript Variable Scope
The scope of a variable is the region of your program in which it is defined. JavaScript
variables have only two scopes.
1. Global Variables: A global variable has global scope which means it can be defined
anywhere in your JavaScript code.
2. Local Variables: A local variable will be visible only within a function where it is
defined. Function parameters are always local to that function.
Javascript Datatypes
One of the most fundamental characteristics of a programming language is the set of data
types it supports. These are the type of values that can be represented and manipulated
in a programming language.
JavaScript allows you to work with three primitive data types:
 Numbers, e.g., 123, 120.50 etc.
 Strings of text e.g. "This text string" etc.
 Boolean e.g., true or false.

Javascript Operators
Operator is a symbol that operates on given data and the variable, that takes action on
operands.
Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and
‘+’ is called the operator. JavaScript supports the following types of operators.
 Arithmetic Operators
 Comparison Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators
Arithmetic operators:-
Arithmetic operators perform arithmetic operation on numbers (literals or variables).
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus(Remainder)
** Exponentiation
The exponentiation operator (**) raises the first operand to the power of
the second operand.
Let z=x**2;
X**y produces the same results as
math.pow(x,y)
let z=math.pow(x,2);
++ Increment
-- Decrement

Comparison Opearators
The JavaScript comparison operator compares the two operands. The comparison
operators are as follows:
Operator Description Example
== Is equal to 10==20=false
=== Identical(equal and of same type) 10==20=false
!== Not equal to 10!=20=true
!== Not Identical 20!==20=false
> Greater than 20>10=true
>= Greater than or equal to 20>=10=true
< Less than 20<10=false
<= Less than or equal to 20<=10=false

Logical operators
The following operators are known as JavaScript logical operators.
Operator Description Example
&& Logical AND (10==20&&20==33)=false
|| Logical OR (10==20||20==33)=false
! Logical NOT !(10==20)=true

Assignment Operators
Assignment operators assign values to JavaScript variables.
Operator Example Same as
= X=Y X=Y
+= X+=Y X=X+Y
-= X-=Y X=X-Y
*= X*=Y X=X*Y
/= X/=Y X=X/Y
%= X%=Y X=X%Y
**= X**=Y X=X**Y

PROGRAMS
1. WAP to find average of the numbers.
<HTML>
<HEAD>
<TITLE>finding average</TITLE>
</HEAD>
<BODY>
<script language=”javascritp” type =”text/javascript”>
Var a,b,c,avg;
a=10;
b=20;
c=15;
avg=(a+b+c)/3;
document.write(avg);
</script>
</BODY>
</HTML>

2. WAP to find greatest number between two different number using javascript.
<Html>
<Head>
<Title>finding greatest number</title>
</Head>
<Body>
<Script language=”javascript” type=”text/javascript”>
Var a,b;
a=5;
b=10;
if(a>b)
{
document.write(greatest number is”+a);
}
Else
{
Document.write(greatest number is”+b);
}
</Script>
</Body>
</Html>

3. WAP to check the given number is positive, negative or equal to zero.


<Html>
<Head>
<Title>to check no. is positive, negative or equal</Title>
</head>
<body.
<Script language=”javascript” type=”text/javascript”>
Var a=0;
If(a>0)
{
Document.write(“given no. is positive”+a);
}
Else if(a==0)
{
Document.write(“given no. is equal to zero”+a);
}
Else
{
Document.write(“given no. is negative”+a);
}
</Script>
</Body>
</Html>

4. WAP to display even nuber from 1 to 10 using javascript.


<Html>
<Head>
<Title> To display even number</Title>
</Head>
<Body>
<script Language=”javascript” type=”text/javascript”>
var I;
for(i=2;i<=10;i+2)
{
document.write(i+”<br>”);
}
</script>
</Body>
</Html>

5. WAP to display sum of two number using prompt method.


<Html>
<Head>
<Title>to display sum</Title>
</Head>
<Body>
<script Language=”javascript” type=”text/javascript”>
var a,b,c;
a=parseInt(prompt(“enter first number”));
b=parseInt(prompt(“enter second number”));
c=a+b;
document.write(“sum is”+c);
</script>
</body>
</Html>

PHP
Definition
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers
to create dynamic content that interacts with databases. PHP is basically used for developing
web-based software applications. PHP started out as a small open-source project that evolved as
more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version
of PHP way back in 1994.
Advantage
 Free to use
 Easy to learn
 Compatible
 Efficient
 Secure
 Database Support
 Extensive library
 Cloud Compatible
Variable Declaration
A variable can have a short name (like x and y) or a more descriptive name ( age,
carname,tota_volume).
Rules for PHP variables:-
 A vriable starts with the $ sign, followed by the name of the variable.
 A variable name must starts with a letter or the underscore character.
 A variable name cannot starts with a number.
 A variable name can only contain alpha-numeric character and underscore (a-z,0-9,and _)
 Variable names are case-sesitive($age and $AGE are two different variable)
Datatype
Variable can store data of different types, and diferent data types can do different things,PHP
supports the following data types:
 PHP String
 PHP Integer
 PHP Float(floating point numbers – also called double)
 PHP Boolean
 PHP Array
 PHP Object
 PHP NULL
 PHP resource
Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
 Conditional assignment operators

Arthmetic operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
Operators Name Example Result
+ Addition $x+$y Sum of $x and $y
- Subtraction $x-$y Difference of $x and $y
* Multiplication $x*$y Product of $x and $y
/ Division $x/$y Quotient of $x nd $y
% Modulus $x%$y Remainder of $x divided by $y
** Exponentiation $X**$y Result of raising $x to the $y’th
power

Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable. The
basic assignment operator in PHP is &quot;=&quot;. It means that the left operand gets set to the
value of the assignment expression on the right.
Assignmment Same as Description
x=y x=y The left operand gets to the value of the
expression on the right
X+=y x=x+y Addition
x-=y x=x-y Subtraction
X*=y x=x*y Multiplication
x/=y x=x/y Division
X%=y x=x%y modulus

Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operators Name Example Results
== Equal $x==$y Returns true if $x is equal to $y
=== Identical $x===$y Return true if $x is equal to $y,and
they are of the same type
!= Not equal $x!=$y Return true if $x is not equal to $y
<> Not equal $x<>$y Return true if $x is not equal to $y
!== Not identical $x!==$y Return true if $x is not equal to $y, or
they are not of the same type
> Greater than $x>$y Return true if $x is greater than $y
< Less than $x<$y Return true if $x is less than $y
>= Greater than or equal to $x>=$y Return true if $x is greater than or
equal to $y
<= Less than or equal to $x<=$y Return true if $x is less than or equal
to $y
 Spaceship $x$y Return an integer less than,equal to,or
greater than zero, depending on iff $x
is less than ,equal to,or greater than
$y,introduced inn PHP 7.

Increment/decrement operators
The PHP increment operators are used to increment a variable&#39;s value.
The PHP decrement operators are used to decrement a variable&#39;s value.
operators Name Description
++$x Pre-increment Increment $x by one, then returns $x
$x++ Post-increment Return $x,then increment $x by one
--$x Pre-decrement Decrement $x by one, then return $x
$x-- Post-decrement Return $x, then decrement $x by one
Logical operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example Result
s
And And $x and $y True if both $x and $y are true
Or Or $x or $y True if either $x or $y is true
Xor Xor $x xor $y True if either $x or $y is true,but not both
&& And $x &&$y True if both $x and $y are true
|| Or $x||$y True if either $x or $y is true
! Not !$x True if $x is not true

String operator
PHP has two operators that are specially designed for strings.
Operator Name Example Result
. Concatenation $txt1.$txt2 Concatenation of $txt1 and $txt2
.= Concatenation $txt1.=$txt2 Appends $txt2 to $txt1
assignment

Program
1. WAP to find sum of two number using PHP.
<Html>
<Head>
<Title>to find sum</Title.
</Head>
<Body>
<? Php
$a=5;
$b=10;
$c=$a+$b;
echo”sum is $c”;
?>
</Body>
</Html>

2. WAP to find sum using text box in PHP.


<Html>
<Head>
<Title> to find sum using textbox</Title>
</Head>
<Body>
<Form method=”POST”>
Firstno.:<input type=”text” name=”n1”><br>
Secondno.:<input type=”text” name=”n2”><br>
<input type=”submit” value=”sum”>
</form>
<?php
$a=$_POST[‘n1’];
$b=$_POST[‘n2’];
$c=$a+$b;
echo”sum is $c”;
?>
</body>
</html>
3. WAP to calculate the intrest using textbox.
<html>
<head>
<title>finding interest</title>
</head>
<body>
<form method=”POST”>
Principle:<input type=”text” name =”P”><br>
Time:<input type=”text” name=”T”><br>
Rate:<input type=”text” name=”R”><br>
<input type=”interest” value=”sum”>
</form>
<? Php
$P=$_POST[‘P’];
$T=$_POST[‘T’];
$R=$_POST[‘R’];
$I=($P+$T+$R)/100;
Echo”Interest is $I”;
?>
</body>
</html>

4. WAP to connect data with database


<html>
<head>
<title>database connection</title>
</head>
<body>
<? Php
$connection =my sqli_connect(“localhost”,”root”,” “,”student”);
if(!$connection)
{
echo”connection failed”my sqli_connect_error();
}
else
{
cho”connection successfully”;
}
?>
</body.
</html>

5. WAP to insert data into database.


<html.
<head>
<title>insert data</title>
</head>
<body.
<? php
$conn=my sqli_connect(“localhost”,”root”,” “,”sectionc4”);
if(!$conn)
{
echo”error occurred during connection”.my sqli_connect_error();
}
else
{
$sql=”insert the sturecord.(rollno,name.value(1,’samir’),(2,’sachin’),(3,’pankaj’)”);
if(mysqli_query($conn,$sql))
{
echo”data inserted successfully”;
}
else{
echo”error is occurred”;
}
?>
</body>
</html>
PHP
Definition
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers
to create dynamic content that interacts with databases. PHP is basically used for developing
web-based software applications. PHP started out as a small open-source project that evolved as
more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version
of PHP way back in 1994.
Advantage
 Free to use
 Easy to learn
 Compatible
 Efficient
 Secure
 Database Support
 Extensive library
 Cloud Compatible
Variable Declaration
A variable can have a short name (like x and y) or a more descriptive name ( age,
carname,tota_volume).
Rules for PHP variables:-
 A vriable starts with the $ sign, followed by the name of the variable.
 A variable name must starts with a letter or the underscore character.
 A variable name cannot starts with a number.
 A variable name can only contain alpha-numeric character and underscore (a-z,0-9,and _)
 Variable names are case-sesitive($age and $AGE are two different variable)
Datatype
Variable can store data of different types, and diferent data types can do different things,PHP
supports the following data types:
 PHP String
 PHP Integer
 PHP Float(floating point numbers – also called double)
 PHP Boolean
 PHP Array
 PHP Object
 PHP NULL
 PHP resource
Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
 Conditional assignment operators

Arthmetic operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
Operators Name Example Result
+ Addition $x+$y Sum of $x and $y
- Subtraction $x-$y Difference of $x and $y
* Multiplication $x*$y Product of $x and $y
/ Division $x/$y Quotient of $x nd $y
% Modulus $x%$y Remainder of $x divided by $y
** Exponentiation $X**$y Result of raising $x to the $y’th
power

Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable. The
basic assignment operator in PHP is &quot;=&quot;. It means that the left operand gets set to the
value of the assignment expression on the right.
Assignmment Same as Description
x=y x=y The left operand gets to the value of the
expression on the right
X+=y x=x+y Addition
x-=y x=x-y Subtraction
X*=y x=x*y Multiplication
x/=y x=x/y Division
X%=y x=x%y modulus

Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operators Name Example Results
== Equal $x==$y Returns true if $x is equal to $y
=== Identical $x===$y Return true if $x is equal to $y,and
they are of the same type
!= Not equal $x!=$y Return true if $x is not equal to $y
<> Not equal $x<>$y Return true if $x is not equal to $y
!== Not identical $x!==$y Return true if $x is not equal to $y, or
they are not of the same type
> Greater than $x>$y Return true if $x is greater than $y
< Less than $x<$y Return true if $x is less than $y
>= Greater than or equal to $x>=$y Return true if $x is greater than or
equal to $y
<= Less than or equal to $x<=$y Return true if $x is less than or equal
to $y
 Spaceship $x$y Return an integer less than,equal to,or
greater than zero, depending on iff $x
is less than ,equal to,or greater than
$y,introduced inn PHP 7.

Increment/decrement operators
The PHP increment operators are used to increment a variable&#39;s value.
The PHP decrement operators are used to decrement a variable&#39;s value.
operators Name Description
++$x Pre-increment Increment $x by one, then returns $x
$x++ Post-increment Return $x,then increment $x by one
--$x Pre-decrement Decrement $x by one, then return $x
$x-- Post-decrement Return $x, then decrement $x by one

Logical operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example Result
s
And And $x and $y True if both $x and $y are true
Or Or $x or $y True if either $x or $y is true
Xor Xor $x xor $y True if either $x or $y is true,but not both
&& And $x &&$y True if both $x and $y are true
|| Or $x||$y True if either $x or $y is true
! Not !$x True if $x is not true

String operator
PHP has two operators that are specially designed for strings.
Operator Name Example Result
. Concatenation $txt1.$txt2 Concatenation of $txt1 and $txt2
.= Concatenation $txt1.=$txt2 Appends $txt2 to $txt1
assignment

Program
6. WAP to find sum of two number using PHP.
<Html>
<Head>
<Title>to find sum</Title.
</Head>
<Body>
<? Php
$a=5;
$b=10;
$c=$a+$b;
Echo”sum is $c”;
?>
</Body>
</Html>

7. WAP to find sum using text box in PHP.


<Html>
<Head>
<Title> to find sum using textbox</Title>
</Head>
<Body>
<Form method=”POST”>
Firstno.:<input type=”text” name=”n1”><br>
Secondno.:<input type=”text” name=”n2”><br>
<input type=”submit” value=”sum”>
</form>
<?php
$a=$_POST[‘n1’];
$b=$_POST[‘n2’];
$c=$a+$b;
Echo”sum is $c”;
?>
</body>
</html>
8. WAP to calculate the intrest using textbox.
<html>
<head>
<title>finding interest</title>
</head>
<body>
<form method=”POST”>
Principle:<input type=”text” name =”P”><br>
Time:<input type=”text” name=”T”><br>
Rate:<input type=”text” name=”R”><br>
<input type=”interest” value=”sum”>
</form>
<? Php
$P=$_POST[‘P’];
$T=$_POST[‘T’];
$R=$_POST[‘R’];
$I=($P+$T+$R)/100;
Echo”Interest is $I”;
?>
</body>
</html>

9. WAP to connect data with database


<html>
<head>
<title>database connection</title>
</head>
<body>
<? Php
$connection =my sqli_connect(“localhost”,”root”,” “,”student”);
If(!$connection)
{
Echo”connection failed”my sqli_connect_error();
}
Else
{
Echo”connection successfully”;
}
?>
</body.
</html>

10. WAP to insert data into database.


<html.
<head>
<title>insert data</title>
</head>
<body.
<? Php
$conn=my sqli_connect(“localhost”,”root”,” “,”sectionc4”);
If(!$conn)
{
Echo”error occurred during connection”.my sqli_connect_error();
}
Else
{
$sql=”insert the sturecord.(rollno,name.value(1,’samir’),(2,’sachin’),(3,’pankaj’)”);
If(mysqli_query($conn,$sql))
{
Echo”data inserted successfully”;
}
Else{
Echo”error is occurred”;
}
?>
</body>
</html>

You might also like