· 6 years ago · Oct 09, 2019, 10:38 AM
1PHP
2BASICS
3<?php
4echo "Hi Kartic";
5?>
6
7<?php
8
9$a="Hi Kartic How are you";
10echo $a;
11
12?>
13
14CONCATENATION
15<?php
16
17$a="Hi Kartic How are you";
18$b="Whatsup";
19
20$c=$a.$b;
21
22echo "$c";
23?>
24MUTUAL CONCATENATION
25<?php
26
27$a="Hi Kartic How are you";
28$a.="Whatsup Then";
29
30echo "$a";
31?>
32
33
34ARITHMETIC OPERATIONS
35<?php
36
37$a=10;
38$b=2;
39
40$c=$a+$b;
41$d=$a-$b;
42$e=$a*$b;
43$f=$a/$b;
44$g=$a%$b;
45$h=$a**2;
46
47echo "Addition is ".$c."<br>";
48 echo "Subtraction is ".$d."<br>";
49 echo "Multiplication is ".$e."<br>";
50 echo "Divisionion is ".$f."<br>";
51echo "Modulus is ".$g."<br>";
52echo "Exponentiation is ".$h."<br>";
53
54?>
55
56DATA TYPES
57<?php
58
59$a=10;
60$b=100.00;
61$c=1.2;
62$d="kabil";
63$e=true;
64$f=array("kartic","kabil");
65
66var_dump($a);
67echo "<br>";
68var_dump($b);
69echo "<br>";
70var_dump($c);
71echo "<br>";
72var_dump($d);
73echo "<br>";
74var_dump($e);
75echo "<br>";
76var_dump($f);
77?>
78
79
80CONTROL FLOW STATEMENTS IN PHP
81(If Else statements)
82<?php
83
84$a=20;
85if($a >18)
86 echo "Eligible to vote";
87else
88 echo "Not Eligible";
89 ?>
90
91
92
93NESTED IF ELSE
94<?php
95
96$a=20;
97$b=30;
98$c=12;
99
100if(($a >$b)and ($a>$c))
101echo "A is greater";
102else if(($b>$a) and($b>$c))
103echo "b is greater";
104else
105echo "C is greater";
106?>
107SWITCH STATEMENTS
108<?php
109
110$actor="TR";
111
112switch($actor)
113{
114 case "ravi":
115 echo "Your favaourite actorr is ravi";
116 break;
117
118 case "ajith":
119 echo "Your favaourite actorr is ajith";
120 break;
121
122 case "TR":
123 echo "Your favaourite actorr is TR";
124 break;
125
126 default:
127 echo "Your favaourite actorr is RAJINI";
128
129}
130
131?>
132
133
134
135
136
137
138
139STRING FUNCTIONS
140<?php
141
142$actor="Vijaya t rajendar ";
143$actor1="Vijay";
144
145$a=strlen($actor);
146$b=strtolower($actor);
147$c=strtoupper($actor);
148$d=ucfirst($actor); //convertinf first charcter to uppercase
149$e=lcfirst($actor);
150$f=ucwords($actor); //convertinf first charcter in each word to uppercase
151$g=strrev($actor);
152
153if(strcmp($actor,$actor1)==0)
154
155 echo "Strings are equal"."<br>";
156else
157
158 echo "Strings are not equal"."<br>";
159
160echo $a;
161echo "<br>";
162echo $b;
163echo "<br>";
164echo $c;
165echo "<br>";
166echo $d;
167echo "<br>";
168echo $e;
169echo "<br>";
170echo $f;
171echo "<br>";
172echo $g;
173echo "<br>";
174
175?>
176
177
178
179
180
181
182
183
184
185MATH FUNCTIONS
186<?php
187
188$num=23.56;
189$num1=-50;
190
191echo "ceiling is ".ceil($num);
192echo "<br>";
193echo "flooring is ".floor($num);
194echo "<br>";
195echo "absolute is ".abs($num1);
196echo "<br>";
197echo "random value is ".rand(1,100);
198echo "<br>";
199echo "random value is ".pow($num1,2);
200echo "<br>";
201
202?>
203
204
205
206
207
208FORMS
209<html>
210<body>
211<form method="post">
212<input type="number" name="number1"/>Enter First Number <br/>
213<input type="number" name="number2"/>Enter Second Number <br/>
214<input type="submit" name="add" value="ADD"/>
215<input type="submit" name="sub" value="SUB"/>
216<input type="submit" name="mul" value="MUL"/>
217<input type="submit" name="div" value="DIV"/>
218
219</form>
220
221<?php
222
223if(isset($_POST["add"]))
224{
225 $a=$_POST['number1'];
226 $b=$_POST['number2'];
227 $c=$a+$b;
228 echo "Sum is ".$c;
229}
230
231if(isset($_POST["sub"]))
232{
233 $a=$_POST['number1'];
234 $b=$_POST['number2'];
235 $c=$a-$b;
236 echo "Subtraction is ".$c;
237}
238if(isset($_POST["mul"]))
239{
240 $a=$_POST['number1'];
241 $b=$_POST['number2'];
242 $c=$a*$b;
243 echo "Multiplication is ".$c;
244}
245if(isset($_POST["div"]))
246{
247 $a=$_POST['number1'];
248 $b=$_POST['number2'];
249 $c=$a/$b;
250 echo "Division is ".$c;
251}
252?>
253</body> </html>
254FILE HANDLING
255(WRITE OPERATION)
256<?php
257
258$file1=fopen("C:/xampp_new/htdocs/ab.txt","w");
259fwrite($file1,"Hello ");
260echo "<br>";
261fwrite($file1,"Kartic ");
262echo "<br>";
263fwrite($file1,"How are you ");
264echo "<br>";
265fwrite($file1,"Whatsup ");
266echo "<br>";
267fclose($file1);
268
269?>
270
271
272
273
274
275
276
277(READ OPERATION)
278
279<?php
280
281$filename="C:/xampp_new/htdocs/ab.txt";
282$file1=fopen($filename,"r");
283$file2=fread($file1,filesize($filename));
284echo $file2;
285fclose($file1);
286
287
288?>
289
290DELETE OEPRATION
291<?php
292
293$filename="C:/xampp_new/htdocs/ab.txt";
294unlink($filename);
295
296echo "file deleted"
297
298?>
299
300CRUD OPERATION
301
302
303DB CONFIGURATION
304<?php
305$host="localhost";
306$dbname="sai";
307$username="root";
308$password="";
309$ab=mysqli_connect($host,$username,$password,$dbname);
310
311if(!$ab)
312 echo "No database is there";
313else
314 echo "Database exists"
315 ?>
316Insert Operation (add.html)
317<html>
318<body>
319<form action="add.php" method="post">
320Enter your id   <input type="text" name="id"/> <br/>
321Enter your name   <input type="text" name="name"/> <br/>
322Enter your age   <input type="number" name="age"/> <br/>
323Enter your email   <input type="text" name="email"/> <br/>
324
325<input type="submit" name="add" value="ADD"/>
326</form>
327</body>
328</html>
329
330ADD.PHP
331<?php
332include_once("1.php");
333if(isset($_POST['add']))
334{
335 $newid=$_POST['id'];
336 $newname=$_POST['name'];
337 $newage=$_POST['age'];
338 $newemail=$_POST['email']; }
339
340$add=mysqli_query($ab,"insert into users(id,name,age,email) values('$newid','$newname','$newage','$newemail')");
341if(!$add)
342 echo "details not added";
343else
344 echo "details added successfully";
345?>
346
347DELETE OPERATION (delete.html)
348<html>
349<body>
350<form action="delete.php" method="get">
351Enter your id   <input type="text" name="id"/> <br/>
352
353<input type="submit" name="delete" value="DELETE"/>
354</form>
355</body>
356</html>
357
358
359
360
361Delete.php
362<?php
363include_once("1.php");
364
365$newid=$_GET['id'];
366$delete=mysqli_query($ab,"delete from users where id='$newid' ");
367if(!$delete)
368 echo "details not deleted";
369else
370 echo "details deleted successfully";
371?>
372
373UPDATE OPERATION
374(update.html)
375<html>
376<body>
377<form action="update.php" method="post">
378Enter your id   <input type="text" name="id"/> <br/>
379Enter your name   <input type="text" name="name"/> <br/>
380Enter your age   <input type="number" name="age"/> <br/>
381Enter your email   <input type="text" name="email"/> <br/>
382<input type="submit" name="update" value="UPDATE"/>
383</form>
384
385</body>
386</html>
387
388Update.php
389<?php
390include_once("1.php");
391if(isset($_POST['update']))
392{
393 $newid=$_POST['id'];
394 $newname=$_POST['name'];
395 $newage=$_POST['age'];
396 $newemail=$_POST['email'];
397}
398
399$update=mysqli_query($ab,"update users set name='$newname',age='$newage',email='$newemail' where id='$newid'");
400if(!$update)
401 echo "details not updated";
402else
403 echo "details updated successfully";
404?>
405
406SELET OPERATION
407(select.html)
408<html>
409<body>
410<form action="select.php" method="get">
411Enter your id   <input type="text" name="id"/> <br/>
412 <input type="submit" name="select" value="SELECT"/>
413</form>
414</body>
415</html>
416
417SELECT.PHP
418<?php
419include_once("1.php");
420
421$newid=$_GET['id'];
422
423$select=mysqli_query($ab,"select name,age,email from users where id='$newid' ");
424while($res = mysqli_fetch_array($select))
425{
426 $id=$res['id'];
427$name = $res['name'];
428 $age = $res['age'];
429 $email = $res['email'];
430}
431
432if(!$select)
433echo "details not retrieved";
434else
435echo "details retrieved successfully";
436?>
437
438<html>
439 <body>
440 <br/><br/>
441 <form method="post" >
442 <table >
443 <tr>
444 <td>Name</td>
445 <td><input type="text" name="name" value="<?php echo $name;?>"></td>
446 </tr>
447
448
449
450 <tr>
451 <td>Age</td>
452 <td><input type="text" name="age" value="<?php echo $age;?>"></td>
453 </tr>
454
455 <tr>
456 <td>Email</td>
457 <td><input type="text" name="email" value="<?php echo $email;?>"></td>
458 </tr>
459
460 </table>
461 </form>
462</body>
463</html>
464
465
466
467
468
469
470
471
472APPLICATION FORM
473<?php
474
475$host="localhost";
476$dbname="sai";
477$username="root";
478$password="";
479$ab=mysqli_connect($host,$username,$password,$dbname);
480
481if(!$ab)
482echo "No database is there";
483else
484echo "Database exists"
485
486?>
487DATABASE SCHEMA
488
489
490
491DATABSE RECORDS
492
493ADD.HTML
494<html>
495<body>
496<header> <center> SAEC COLLEGE RAMANATHAPURAM </center> </header>
497<form action="add.php" method="post">
498Enter your id   <input type="text" name="id"/> <br/>
499Enter your name   <input type="text" name="name"/> <br/>
500
501Enter your Gender Information
502<input type="radio" name="gender" value="male"/>Male
503<input type="radio" name="gender" value="Female"/>Female <br/>
504
505Enter your Educational Information
506<input type="radio" name="qualification" value="sslc"/>SSLC
507<input type="radio" name="qualification" value="hsc"/>HSC<br/>
508
509Enter your studying year <br/>
510<input type="checkbox" name="year" value="1" /> 1 <br />
511<input type="checkbox" name="year" value="2" /> 2 <br />
512<input type="checkbox" name="year" value="3" /> 3 <br />
513<input type="checkbox" name="year" value="4" /> 4 <br />
514
515Enter the Arrear Details <br/>
516<select name="arrears">
517
518<option value="">SELECT</option>
519<option value="nil">NIL</option>
520<option value="less than 2">less than 2</option>
521<option value="greater than 2">greater than 2</option>
522</select> <br/> <br/> <br/>
523
524Any Others about yourself
525<textarea name="others" rows="5" cols="40">
526</textarea>
527
528<br/> <br/> <br/> <br/>
529<input type="submit" name="add" value="ADD"/> <br/>
530<footer> <center> By Kabilesh </center> </footer>
531</form>
532
533</body>
534</html>
535ADD.PHP
536<?php
537include_once("1.php");
538if(isset($_POST['add']))
539{
540$newid=$_POST['id'];
541$newname=$_POST['name'];
542$newgender=$_POST['gender'];
543$newqualification=$_POST['qualification'];
544$newyear=$_POST['year'];
545$newarrears=$_POST['arrears'];
546$newothers=$_POST['others'];
547}
548
549$add=mysqli_query($ab,"insert into success(id,name,gender,qualification,year,arrears,others) values('$newid','$newname','$newgender','$newqualification','$newyear','$newarrears','$newothers')");
550if(!$add)
551echo "details not added";
552else
553echo "details added successfully";
554
555?>
556OUTPUT
557
558
559
560
561
562
563
564
565
566
567
568
569PDF GENERATOR
570<?php
571require('fpdf/fpdf.php');
572
573$pdf = new FPDF();
574$pdf->AddPage();
575$pdf->SetFont('Times','I',32);
576$pdf->Cell(40,10,'Hello Kartic!');
577$pdf->Output();
578?>
579
580OUTPUT
581
582
583
584
585
586PDF GENERATOR USING (FPDF)
587<?php
588
589require_once("DBcontroller.php");
590$db_handle = new DBController();
591$result = $db_handle->runQuery("SELECT * FROM toy");
592
593require('fpdf/fpdf.php');
594$pdf = new FPDF();
595$pdf->AddPage();
596$pdf->SetFont('Arial','B',12);
597
598foreach($result as $row)
599{
600$pdf->SetFont('Arial','',12);
601$pdf->Ln();
602foreach($row as $column)
603$pdf->Cell(90,12,$column,1);
604}
605ob_start();
606$pdf->Output();
607 ob_end_flush();
608?>
609DATABASEHANDLER.PHP
610<?php
611class DBController {
612 private $host = "localhost";
613 private $user = "root";
614 private $password = "";
615 private $database = "sai";
616
617 private static $conn;
618
619 function __construct()
620{
621 $this->conn = $this->connectDB();
622 if(!empty($this->conn))
623{
624 $this->selectDB();
625 }
626 }
627
628
629
630
631
632 function connectDB()
633{
634 $conn = mysqli_connect($this->host,$this->user,$this->password,
635 $this->database);
636
637 return $conn;
638 }
639
640 function selectDB()
641{
642 mysqli_select_db($this->conn, $this->database);
643 }
644
645 function runQuery($query)
646{
647 $result = mysqli_query($this->conn, $query);
648 while($row=mysqli_fetch_assoc($result))
649{
650 $resultset[] = $row;
651 }
652 return $resultset;
653 }}
654?>
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669CONVERT .TXT FILE -> PDF
670<?php
671require('fpdf/fpdf.php');
672$pdf = new FPDF();
673$pdf->AddPage();
674$row=file('films.txt');
675$pdf->SetFont('Arial','B',12);
676foreach($row as $rowValue)
677{
678$data=explode(';',$rowValue);
679foreach($data as $columnValue)
680$pdf->Cell(90,12,$columnValue,1); // parameters are w,h,name,border
681 $pdf->Ln(); // New Line
682}
683ob_start();
684$pdf->Output();
685ob_end_flush();
686?>
687
688
689
690
691
692FILMS.TXT (It should be stored in htdocs)
693actorname;filmname
694ajith;villan
695vijay;sura
696surya;24
697OUTPUT
698
699
700
701
702
703
704
705
706
707
708
709
710SENDING SMS USING TEXTLOCAL API
7111) Download the github file named “sourabhkumar github ”
7122) Unzip the folder and copy the files into htdocs folder.
713
7143) Then go to sendsms.php file and make some modifications like this.
715
716<?php
717require('textlocal.class.php');
718
719$textlocal = new Textlocal(false, false,'0HDi08iB/PE-A1xcACI7gn1irYVAZSVfjOhvdQATAo');
720
721$numbers = array(8667380290);
722//NAME OF THE PEOPLE TO SEND SMS
723$sender = 'TXTLCL';
724//TXTLCL SHOULD NOT BE CHANGED
725$message = 'Hello This is Kar How are you';
726// we can provide whichever we like
727try
728{
729 $result = $textlocal->sendSms($numbers, $message, $sender);
730 print_r($result);
731}
732catch (Exception $e)
733 {
734 die('Error: ' . $e->getMessage());
735}
736 ?>
737
738
739
740
741
742
743OTP VERIFICATION USING LINK LOCAL
744
7451) In the Credentials.php add the API key from textlocal and add your mobile number in Credentials.php file.
746
7472) Type in the Web Browser as
748 http://localhost/SMS/index.php
749
750
7513) Here SMS is the name of the folder which contains all the files.