Skip to content

Instantly share code, notes, and snippets.

View niravmadariya's full-sized avatar
💭
I may be slow to respond.

Nirav Madariya niravmadariya

💭
I may be slow to respond.
View GitHub Profile
@niravmadariya
niravmadariya / MySQL-in-app-connection.php
Created November 10, 2018 18:05
MySQL-in-app connection File
<?php
$conn = getenv("MYSQLCONNSTR_localdb"); // this will return the whole connectionstring in a single string
$conarr2 = explode(";",$conn); // Let's beautify it, by splitting it and decorating it in an array
$conarr = array();
foreach($conarr2 as $key=>$value){
$k = substr($value,0,strpos($value,'='));
$conarr[$k] = substr($value,strpos($value,'=')+1);
}
print_r($conarr); // $conarr is an array of values of connection string
?>
@niravmadariya
niravmadariya / sendmail.php
Last active December 24, 2019 16:28
Send mails using PHPMailer
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'PHPMailer/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
@niravmadariya
niravmadariya / SignupForm.php
Created June 29, 2018 07:11
Signup Form with Captcha
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign Up Form</title>
</head>
<body>
<section>
<form class="c-form" action="" method="POST">
<div>
@niravmadariya
niravmadariya / Arrays.php
Created June 7, 2018 07:49
Numerical and Associative Array Example in php
<?php
$a[0] = 1; // Numeric Array
$a[1] = 2;
$a[2] = 3;
$a[3] = 4;
$a[4] = 5;
$b['a']='a'; // Associative Array
$b['b']='b';
$b['c']='c';
$b['d']='d';
@niravmadariya
niravmadariya / lock.php
Created June 7, 2018 07:35
Lock a file in php
<?php
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX))
{
fwrite($file,"Write something");
// release lock
flock($file,LOCK_UN);
}
else
@niravmadariya
niravmadariya / Copyfile.php
Last active June 7, 2018 07:31
Copy file content to another file - PHP
<?php
echo copy("source.txt","target.txt");
//Another way
//Get all the content from the file
$fileContents = file_get_contents('1.txt');
//Output to new file
$fh = fopen('output.txt', 'w+');
file_put_contents($fh, $fileContents);
fclose($fh);
@niravmadariya
niravmadariya / EvenOdd.php
Created June 7, 2018 07:04
Given Number is Even or Odd
<html>
<head><title>Check if the number is Even or Odd</title></head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST['submit']){
<html>
<head><title>Reverse of a given Number in PHP</title></head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST['submit']){
@niravmadariya
niravmadariya / FactorialProgram.php
Last active June 7, 2018 07:01
Factorial Program in php
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
@niravmadariya
niravmadariya / GreetUser.php
Last active June 7, 2018 07:02
Greet user based on time
<?php
date_default_timezone_set('Asia/Kolkata');
$hour=date('h', time());
$ampm=date('a', time());
if($ampm=="am"){
if($hour>=1 && $hour<=11){
echo "Good Morning!!!";
}
}