1. Send Mail from Localhost :
2. How to redirect from http to https using htaccess
file
ANS: add below code in your .htaccess file
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST}
!^www\. [NC]
RewriteCond %{HTTP_HOST}
^(.*)$ [NC]
RewriteRule (.*)
https://www.domainname.com/$1 [R=301,L]
(or)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$
https://www.yourdomain.com/$1 [R,L]
3. url redirection using htaccess file..
RedirectMatch 301 ^/landingpage/gdpr.php
https://www.wenso.co.uk/gdpr
4. Remove space in the footer
ANS:
html, body {
margin: 0;
padding: 0;
height: 100%;
}
html {
overflow-y: hidden;
}
Ans: add below line of code in your .htaccess file.
DirectoryIndex home.php
6.How to print 100 numbers with out using loop ?
Ans:
$no = 1;
STATEMENT:
if($no<15){
echo $no++."<br/>";
goto STATEMENT;
}
//=============== Or =====================
$val=1;
printval($val);
function printval($val){
if($val<=50){
echo $val. "<br/>" ;
$val++;
printval($val);
}
}
//============= Or =======================
class printer {
public function __construct() {
$this->print1();
}
public function __call($method, $_) {
$count = str_replace('print', '', $method);
echo "$count ";
$this->{"print" . ++$count}();
}
}
class thousand_printer extends printer {
public function print1000() {}
}
new thousand_printer;
7. Factory Design Pattern
Class Automobile {
private $vehicleMake;
private $vehicleModel;
public function __construct($make,$model){
$this->vehicleMake=$make;
$this->vehicleModel=$model;
}
public function getMakeAndModel(){
Return $this->vehicleMake." - ".$this->vehicleModel;
}
}
Class AutomobileFactory {
Public static function create($make,$model) {
return new Automobile($make,$model);
}
}
$veyron =AutomobileFactory::create("Bugatti","Veyron");
Print_r($veyron->getMakeAndModel());
(OR)
// Abstract Database Connection
interface DatabaseConnection {
public function connect();
}
// Concrete Database Connections
class MySqlConnection implements DatabaseConnection {
private $host;
private $username;
private $password;
private $connection;
public function __construct($host, $username, $password) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
}
public function connect() {
try {
$dsn = "mysql:host={$this->host};dbname=mydb";
$this->connection = new PDO($dsn, $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected to MySQL database on {$this->host}.\n";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
}
public function getConnection() {
return $this->connection;
}
}
class PostgreSqlConnection implements DatabaseConnection {
private $host;
private $username;
private $password;
private $connection;
public function __construct($host, $username, $password) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
}
public function connect() {
try {
$dsn = "pgsql:host={$this->host} dbname=mydb user={$this->username} password={$this->password}";
$this->connection = new PDO($dsn);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected to PostgreSQL database on {$this->host}.\n";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
}
public function getConnection() {
return $this->connection;
}
}
class DatabaseConnectionFactory {
public static function createConnection($dbType, $host, $username, $password) {
switch ($dbType) {
case 'mysql':
return new MySqlConnection($host, $username, $password);
case 'pgsql':
return new PostgreSqlConnection($host, $username, $password);
default:
throw new InvalidArgumentException("Unsupported database type: $dbType");
}
}
}
// Rest of the code remains the same
// Client Code
$mysqlConnection = DatabaseConnectionFactory::createConnection('mysql', 'localhost', 'root', 'password');
$mysqlConnection->connect();
$mysqlDb = $mysqlConnection->getConnection();
// Use $mysqlDb to perform database operations
$pgsqlConnection = DatabaseConnectionFactory::createConnection('pgsql', 'localhost', 'postgres', 'password');
$pgsqlConnection->connect();
$pgsqlDb = $pgsqlConnection->getConnection();
// Use $pgsqlDb to perform database operations
8.Singleton Design Pattern:
class Singleton {
private static $instance;
private function __construct()
{
// Private constructor to prevent instantiation from outside
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
// Other methods of the Singleton class can be defined here
}
$singletonInstance = Singleton::getInstance();
$singletonInstance2 = Singleton::getInstance();
if ( $singletonInstance1 === $singletonInstance2 ) {
echo "Singleton works, both variables contain the same instance.";
} else {
echo "Singleton failed, variables contain different instances.";
}
(or)
Class MyClass{
private static $instance = null;
public $mix;
private function __construct(){} //initialize the variables at the time of object creation
public function __isset($name) {
echo "Non-existent property '$name'";
} //it checks weather the variable is declared or not
public function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->data[$name]);
} //
public function __invoke(){}
public static function getInstance(){
if(self::$instance ==null){
self::$instance = new MyClass();
}
return self::$instance;
}
}
$obj1 = MyClass::getInstance();
$obj2 = MyClass::getInstance();
$obj1->mix="Car";
$obj2->mix="Bus";
//echo "<pre>";
print_r($obj1->mix);
//echo "<pre>";
print_r($obj2->mix);
10. What is difference between && and "and" operator in php ?
Ans : both are having different precedence levels and slightly different behaviors. It has a lower precedence than &&. This means that expressions involving and are evaluated after expressions involving &&.
$x = true;
$y = false;
$result = $x and $y; // $result is true
$result = $x && $y; // $result is false
In this case, and has lower precedence than =, so the assignment happens first. As a result, $x is assigned the value true, and then the result of $x and $y is computed. Since the left operand is true, the right operand is not evaluated, and the result is true.
11. Facade Design Pattern:
Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework.
class Facade
{
protected $subsystem1;
protected $subsystem2;
public function __construct(
Subsystem1 $subsystem1 = null,
Subsystem2 $subsystem2 = null
) {
$this->subsystem1 = $subsystem1 ?: new Subsystem1();
$this->subsystem2 = $subsystem2 ?: new Subsystem2();
}
public function operation(): string {
$result = "Facade initializes subsystems:\n";
$result .= $this->subsystem1->operation1();
$result .= $this->subsystem2->operation1();
$result .= "Facade orders subsystems to perform the action:\n";
$result .= $this->subsystem1->operationN();
$result .= $this->subsystem2->operationZ();
return $result;
}
}
/**
* The Subsystem can accept requests either from the facade or client directly.
* In any case, to the Subsystem, the Facade is yet another client, and it's not
* a part of the Subsystem.
*/
class Subsystem1
{
public function operation1(): string {
return "Subsystem1: Ready!\n";
}
public function operationN(): string {
return "Subsystem1: Go!\n";
}
}
/**
* Some facades can work with multiple subsystems at the same time.
*/
class Subsystem2 {
public function operation1(): string {
return "Subsystem2: Get ready!\n";
}
public function operationZ(): string {
return "Subsystem2: Fire!\n";
}
}
function clientCode(Facade $facade) {
echo $facade->operation();
}
$subsystem1 = new Subsystem1();
$subsystem2 = new Subsystem2();
$facade = new Facade($subsystem1, $subsystem2);
clientCode($facade);
Imagine you have a complex process like making a cup of coffee using machine. Making coffee involves several steps, like grinding coffee beans, boiling water, and brewing. These steps can be seen as different subsystems.
Subsystem 1: Grinding Coffee Beans Subsystem 2: Boiling WaterTo make this process easier, you use a coffee maker (the Facade) to simplify the entire process. The coffee maker knows how to grind beans and boil water. You, as the user, don't need to worry about the details of grinding and boiling. You just press a button on the coffee maker. Now, think of it in terms of code: The Facade is like the coffee maker. It knows how to operate the subsystems. Subsystem1 and Subsystem2 are like the components of the coffee maker—the grinder and the heating element, respectively. They can do their tasks but are complex on their own.
clientCode() is like you, the user, pressing a button on the coffee maker.
So, in your code: The Facade simplifies interactions with complex subsystems (Subsystem1 and Subsystem2).clientCode() uses the Facade to perform a set of operations without needing to understand the complexity of the subsystems.
This pattern is all about simplifying things for the user (the client) by providing a convenient and easy-to-use interface (the Facade) to interact with a complex system made up of various parts (the subsystems).
12. What is difference between singleton and factory design pattern?
Ans:
Singleton is used when we need a instance of an object .. and it will only one instance can be created. Factory is used when we need to create instances for different classes at run time.
Singleton is used when we need a instance of an object .. and it will only one instance can be created. Factory is used when we need to create instances for different classes at run time.