Most Popular Programming Languages

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

Most Popular programming

languages
Most Popular programming
languages
Most-Used Programming Languages
• To find out what programming languages are
most used today, we looked at GitHub pushes.
Not surprisingly, a lot of legacy languages top
this list. Here are the top 20 programming
languages by total GitHub pushes:

• https://stackify.com/trendiest-programming-
languages-hottest-sought-programming-
languages-2017/
• Based on job listings on Indeed as of March
2017, here are the top 10 programming
languages currently in demand among
companies seeking developer talent:
Working with Databases

PHP and MySQL


Introduction to PHP
Google Trends - JavaScript
Google trends - PHP
• A typical web database transaction consists of the following stages,
which are numbered in the Diagram:

1. A user’s web browser issues an HTTP request for a particular web


page.
– For example, using an HTML form, he might have requested a
search for all books at MikkeliOnlineProfessionalBooks.com
written by Leila Karjalainen.
– The search results page may be called results.php.

2. The web server receives the request for results.php, retrieves the
file, and passes it to the PHP engine for processing.

11
3. The PHP engine begins parsing the script.
– Inside the script is a command to connect to the database and
execute a query (perform the search for books).
– PHP opens a connection to the MySQL server and sends on the
appropriate query.

4. The MySQL server receives the database query,


processes it, and sends the results - a list of books - back to
the PHP engine.

5. The PHP engine finishes running the script, which usually


involves formatting the query results nicely in HTML.
It then returns the resulting HTML to the web server.
12
6. The web server passes the HTML back to the browser, where
the user can see the list of books she requested.

• The above described process is basically the same regardless of


which scripting engine or database server you use.
• Sometimes the web server, PHP engine, and database server all
run on the same machine.
• However, it is quite common for the database server to run on a
different machine.
• You might do this for reasons of security, increased capacity, or
load spreading.
• From a development perspective, this approach is much the same
to work with.

13
1

Browser 6
Web Server

5 2

MySQL Server 4
PHP Engine

14
XAMPP
• Install XAMPP to the root directory: C:\XAMPP
• Launch XAMPP control panel and start the
following services:
MySql
Apache
• Open your web browser and enter in the URL
box:
http://localhost
Running XAMPP Error: Object not
found
• Go to XAMPP Control Panel
• Click on config (APACHE)
• Select Apache(httpd.conf)
• Search for “DocumentRoot”
• Look at the path pointing to the htdocs folder.
e.g "C:/xampp/htdocs“
Or "C:/Users/xampp/htdocs"
Your first PHP application
• Navigate to “C:\XAMPP\htdocs” echo'Hello World';
echo"Hello PHP";
• Create a project folder called Print "Hello PHP World";
HelloPHP Print "GoodBye World!";
• Inside the folder create a file with ?>
a .php extension called Hello
World. </body>
• Write the following code into the </html>
file:

<!DOCTYPE html> • Go to the browser


<html>
<head>
• In the URL box type “localhost”
<title>Web Form</title> • From the folder list select the
<style> project folder created above and
</style> click on the Hello World file
created before.
</head>
<body>
<?php
What is PHP?
• PHP == ‘Hypertext Preprocessor’
• Open-source, SERVER-SIDE scripting language
• PHP scripts reside between reserved PHP tags
• The option is available to embed PHP scripts within HTML pages
• Executed on the server-side
• Source-code not visible by client
– ‘View Source’ in browsers does not display the PHP code
• Supports procedural and object-oriented paradigm (to some
degree)
• All PHP statements end with a semi-colon
• Each PHP script must be enclosed in the reserved PHP tag
What does PHP code look like?
• Structurally similar to C/C++
• Each PHP script must be enclosed in the reserved PHP tag
Variables in PHP
• PHP variables must begin with a “$” sign
• Case-sensitive ($Foo != $foo != $fOo)
• Global and locally-scoped variables
– Global variables can be used anywhere
– Local variables restricted to a function or class
• Certain variable names reserved by PHP
– Form variables ($_POST, $_GET)
– Server variables ($_SERVER)
– Etc.
Variable usage
Echo
• The PHP command ‘echo’ is used to output the
parameters passed to it, typically to send data to
the client’s web-browser
Echo example

• Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25
• Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP
• This is true for both variables and character escape-sequences (such as
“\n” or “\\”)
Print
• Echo has no return value while print has a
return value of 1 so it can be used in
expressions.
• echo can take multiple parameters (although
such usage is rare) while print can take one
argument.
• The echo statement can be used with or
without parentheses: echo or echo().
Echo with HTML
• <?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with
multiple parameters.";
?>
Example
• <?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>$txt1</h2>";
echo "Study PHP at $txt2<br>";
echo $x + $y;
?>
Example - Print
• <?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Example - Print
• <?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
Arithmetic Operations

• $a - $b // subtraction
• $a * $b // multiplication
• $a / $b // division
• $a += 5 // $a = $a+5 Also works for *= and /=
Concatenation
• Use a period to join strings into one.
Escaping the Character
• If the string has a set of double quotation
marks that must remain visible, use the \
[backslash] before the quotation marks to
ignore and display them.
• Run the php script below from the browser as
localhost.
PHP Control Structures
Control Structures: Are the structures within a language that
allow us to control the flow of execution through a program or
script.
Grouped into conditional (branching) structures (e.g. if/else)
and repetition structures (e.g. while loops).
Example if/else if/else statement:

if ($foo == 0) {
echo ‘The variable foo is equal to 0’;
}
else if (($foo > 0) && ($foo <= 5)) {
echo ‘The variable foo is between 1 and 5’;
}
else {
echo ‘The variable foo is equal to ‘.$foo;
}
If ... Else...
• If (condition)
{
Statements;
}
Else
{
Statement;
} No THEN in PHP
While Loops
• While (condition)
{
Statements;
}
Date Display

• Or
Month, Day & Date Format Symbols
M Jan
F January
m 01
n 1

Day of Month d 01
Day of Month J 1
Day of Week l Monday
Day of Week D Mon
Functions

• Functions MUST be defined before they can be called


• Function headers are of the format

function functionName($arg_1, $arg_2, …, $arg_n)

• Unlike variables, function names are not case sensitive (foo(…)


== Foo(…) == FoO(…))
Functions example
<html> function sum($x, $y)
<head> {
<title>Web Form</title> $z = $x + $y;
<style> return $z;
</style> }

</head> writeMsg(); // call the function


<body>
echo "5 + 10 = " . sum(5, 10) . "<br>";
<?php echo "7 + 13 = " . sum(7, 13) . "<br>";
function writeMsg() echo "2 + 4 = " . sum(2, 4);
{ ?>
echo "Hello world!";
} </body>
</html>
Working with forms
• Index.php

• php2.php
Practice questions

• Write a web page that will declare 2 values, find


the product, sum, difference and division of the
values and print the result on the form.

• Alter the program so that the user supplies the 2


values in textboxes, and the desired operation.
• Using a while loop, draw the
following pattern onto the
browser using a PHP script.
Working with databases
From this screen:

• Click on phpMyadmin
• Click on Databases
• Enter db name (myDB) and click on create
• Click on the database name(to the left) and enter a table name
Students, specify 3 columns for it and click on Go.
• Enter 3 column names (name, Surname and RegNo) of type
VarChar and length 20 and click on Save
• Click on SQL,delete the query that’s appearing and enter the
following query:

INSERT INTO ‘Students’(‘name’,’Surname’, ‘RegNo’) VALUES


("Tom","Scott","W101010")

Click on Go.
Working on a database from the browser
• Create a new notepad document,
TableStudents.php and save it in
C:\XAMPP\htdocs
• Open the document and save the following
information
1. Viewing database content in
browser
Viewing Database contents from
browser
<?php $table .="</table>";
// connect to database
if(!$con=mysql_connect("127.0.0.1","root","","myD ?>
B"))
{ <html>
die("Error connecting to database"); <head><title></title></head>
exit(); <body>
} <fieldset>
mysql_select_db("myDB"); <legend>List of Students</legend>
<?php
//selecting from the database echo "<br><br><u><b>IS
223</b></u><br></br>";
$sql="select name from Students";
$rs=mysql_query($sql); //reading contents of variable
$table="<table border='1'>";
$table .="<tr><td><b>Student</b></td></tr>"; echo $table;
?>
//storing data into variable
</fieldset>
while($row = mysql_fetch_array($rs)){ </body>
</html>
$table.="<tr><td>".$row["name"]."</td>
</tr>";
}
Viewing DB from browser
• Go to browser
• Type in the URL Box:
localhost/TableStudents.php
• The following page should appear in browser:
2. Inserting values into database from
browser
Inserting values into DB from browser
Command Syntax:

INSERT INTO table_name ( Columns ) VALUES ( values


for columns)
Inserting values from Browser :
DBInsert.html
<html>

<body>

<h1>A small example page to insert some data in to the MySQL database using PHP</h1>

<form action="insert.php" method="post">

Firstname: <input type="text" name="fname" /><br><br>

Lastname: <input type="text" name="lname" /><br><br>

Reg #: &nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="reg" /><br><br>

<input type="submit" />

</form>
</body>
</html>

Run the form from the browser.


Insert.php
<html>
<body>
<?php
$con = mysql_connect("127.0.0.1","root","",“myDB");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("myDB", $con);

$sql="INSERT INTO students (name, Surname, RegNo)


VALUES
('$_POST[fname]','$_POST[lname]','$_POST[reg]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
3. Run the file TableStudents.php from the
browser.
3. Searching the database content
from browser
Searching from Database
• Question:

Write the PHP code to search from the database while


viewing the results in a table?
4. Editing information in database
from browser
Editing Information in a table
• Question:

Design a form and write the code that would


enable you to edit and update information in
your database table?
5. A Log-In Page
Home.php
<html>

<head>
<title> Log In </title>
</head>
<body>
<div class="login-page">
<div class="form">
<form class="login-form" method="POST" action="Login.php">
<input type="text" placeholder="Student Name" name="user"/>
<input type="password" placeholder="Registration Number" name="pass"/>
<button>login</button>
<p class="message">Not registered?
<a href="#">Create an account</a>
</p>
</form>
</div>
</div>
</body>
</html>
LogIn.php
<?php echo "SUCCESSFULLY LOGIN TO USER PROFILE
PAGE...";
$con = mysql_connect("127.0.0.1","root","","myDB"); header("Location: AfterLogIn.php");
if (!$con) }
{ else
die('Could not connect: ' . mysql_error()); {
} echo "SORRY... YOU ENTERD WRONG ID AND
else PASSWORD... PLEASE RETRY...";
{ }
echo "Connection successful"; }
}
mysql_select_db("myDB", $con); ?>

session_start(); //starting the session for user profile


page
if(!empty($_POST['user'])) //checking the 'user' name
which is from Sign-In.html, is it empty or have some
text
{
$query = mysql_query("SELECT * FROM students
where Name = '$_POST[user]' AND RegNo =
'$_POST[pass]'") or die(mysql_error());
$row = mysql_fetch_array($query) or
die(mysql_error());
if(!empty($row['Name']) AND !empty($row['RegNo']))
{ $_SESSION['Name'] = $row['RegNo'];
AfterLogIn.php

<html>

<body>

<p>
Congratulations for successfully logging into the dummy
web-site.
</p>

</body>
</html>
Practice Questions
Question
a) Create the interface shown below and use it to
insert some data into a database and to retrieve &
view the data in a table:
b) Edit the form on the previous page so that the
user can click on a button labelled Edit.
• When this button is clicked, a modal appears
containing textboxes that allows the user to
edit the information in the database.

You might also like