php operators

PHP Operator

  • February 6, 2019

In this tutorial you will learn about PHP operator, how to manipulate or perform the operations on variables and values using operators in PHP.

An operator takes at least one qualities, which are known as operands, and performs task on them, for example, including them together.

What is PHP Operator

Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an operator that tells PHP to add two variables or values, while the greater-than (>) symbol is an operator that tells PHP to compare two values.

Types of operators

The following lists describe the different operators used in PHP.

PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here’s a complete list of PHP’s arithmetic operators:

Operator Description

+ 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 and $y
% Modulus $x % $y Remainder of $x divided by $y
The following example will show you these arithmetic operators in action:

Example
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14
echo($x - $y); // 0utputs: 6
echo($x * $y); // 0utputs: 40
echo($x / $y); // 0utputs: 2.5
echo($x % $y); // 0utputs: 2
?>

PHP Assignment Operators

The assignment operators are used to assign values to variables.

Operator Description Example Is The Same As
= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x – $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign quotient $x /= $y $x = $x / $y
%= Divide and assign modulus $x %= $y $x = $x % $y
The following example will show you these assignment operators in action:

Example
<?php
$x = 10;
echo $x; // Outputs: 10

$x = 20;
$x += 30;
echo $x; // Outputs: 50
 
$x = 50;
$x -= 20;
echo $x; // Outputs: 30
 
$x = 5;
$x *= 25;
echo $x; // Outputs: 125
 
$x = 50;
$x /= 10;
echo $x; // Outputs: 5
 
$x = 100;
$x %= 15;
echo $x; // Outputs: 10
?>

PHP Comparison Operators

The comparison operators are used to compare two values in a Boolean fashion.

Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
< Less than $x < $y True if $x is less than $y

Greater than $x > $y True if $x is greater than $y
= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y
The following example will show you these comparison operators in action:

Example
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z);  // Outputs: boolean true
var_dump($x === $z); // Outputs: boolean false
var_dump($x != $y);  // Outputs: boolean true
var_dump($x !== $z); // Outputs: boolean true
var_dump($x < $y);   // Outputs: boolean true
var_dump($x > $y);   // Outputs: boolean false
var_dump($x <= $y);  // Outputs: boolean true
var_dump($x >= $y);  // Outputs: boolean false
?>

PHP Increment and Decrement Operators

The increment/decrement operators are used to increment/decrement a variable’s value.

Operator Name Effect
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
–$x Pre-decrement Decrements $x by one, then returns $x
$x– Post-decrement Returns $x, then decrements $x by one
The following example will show you these increment and decrement operators in action:

Example
<?php
$x = 10;
echo ++$x; // Outputs: 11
echo $x;   // Outputs: 11
 
$x = 10;
echo $x++; // Outputs: 10
echo $x;   // Outputs: 11
 
$x = 10;
echo --$x; // Outputs: 9
echo $x;   // Outputs: 9
 
$x = 10;
echo $x--; // Outputs: 10
echo $x;   // Outputs: 9
?>

PHP Logical Operators

The logical operators are typically used to combine conditional statements.

Operator Name Example Result
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
The following example will show you these logical operators in action:

Example
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>

PHP String Operators

There are two operators which are specifically designed for strings.

Operator Description Example Result
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1
The following example will show you these string operators in action:

Example
<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Outputs: Hello World!
 
$x .= $y;
echo $x; // Outputs: Hello World!
?>

PHP Array Operators

The array operators are used to compare arrays:

Operator Name

+ Union $x + $y Union of $x and $y
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y
The following example will show you these array operators in action:

Example
"Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y); // Outputs: boolean false
var_dump($x === $y); // Outputs: boolean false
var_dump($x != $y); // Outputs: boolean true
var_dump($x <> $y); // Outputs: boolean true
var_dump($x !== $y); // Outputs: boolean true
?>

PHP Spaceship Operator PHP 7

PHP 7 introduces a new spaceship operator (<=>) which can be used for comparing two expressions. It is also known as combined comparison operator.

The spaceship operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It basically provides three-way comparison as shown in the following table:

Operator <=> Equivalent
$x < $y ($x <=> $y) === -1
$x <= $y ($x <=> $y) === -1 || ($x <=> $y) === 0
$x == $y ($x <=> $y) === 0
$x != $y ($x <=> $y) !== 0
$x >= $y ($x <=> $y) === 1 || ($x <=> $y) === 0
$x > $y ($x <=> $y) === 1
The following example will show you how spaceship operator actually works:

Example
<?php
// Comparing Integers 
echo 1 <=> 1; // Outputs: 0
echo 1 <=> 2; // Outputs: -1
echo 2 <=> 1; // Outputs: 1
 
// Comparing Floats
echo 1.5 <=> 1.5; // Outputs: 0
echo 1.5 <=> 2.5; // Outputs: -1
echo 2.5 <=> 1.5; // Outputs: 1
 
// Comparing Strings
echo "x" <=> "x"; // Outputs: 0
echo "x" <=> "y"; // Outputs: -1
echo "y" <=> "x"; // Outputs: 1
?>

Read our next article to know PHP database connection

Source credit : https://www.tutorialrepublic.com/php-tutorial/php-operators.php

payment gateway in php

Payment Gateway in PHP

  • November 26, 2018

Abstract

The assignment of the Web Payments Working Group will be to create payments easier and safer on the Web. In this instructional exercise, we are going to see how to incorporate a payment gateway in PHP. We are going to see a precedent code in PHP for payment mix.

The group is also chartered to come up with numerous technologies. This document describes an architecture and a pair of functions within that structure which, together, ease the group’s assignment.

This blatantly refers to components by their own role, recognizing that different implementations might come in numerous functions being embraced by one system component. No particulars are defined about the way the works of each role is going to probably be fulfilled beyond the interior calculations which every needs to implement. Specific interfaces might be clarified at length in accompanying documents define these functions have been employed inside those contexts.NOTE

This structure is centered on the connections between a payer and payee on the Web and will not pay for the components or architecture needed for the entire endtoend payment procedure.

Status of This Document

This document is draft of a potential specification. It’s no official status of any sort and doesn’t represent the support or consensus of almost any standards organization.

The working group maintains a list of all bug reports that the group has not yet addressed. This draft highlights some of those impending issues which remain to be discussed in the working group. No decision was taken on the outcome of these issues including whether or not they are valid. Pull requests with proposed specification text for exceptional problems are strongly encouraged.

This specification was derived from A Payments Initiation Architecture for the Web.

Table of Contents

  1. 1.Design Decisions
    1. 1.1Improve the payment experience for payers
    2. 1.2Protect payer privacy
  2. 2.Definitions
  3. 3.Conformance
  4. 4.Payment Request Flow
  5. 5.Payment Methods
  6. 6.Roles
    1. 6.1Payment App
      1. 6.1.1Supported vs Enabled Payment Methods
      2. 6.1.2Algorithms
        1. 6.1.2.1Processing Payment Requests
        2. 6.1.2.2Returning a list of enabled Payment Methods
      3. 6.1.3Extension Points
        1. 6.1.3.1Selecting a Payment Method
        2. 6.1.3.2Performing Payment Method Specific Processing
    2. 6.2Payment Mediator
      1. 6.2.1Algorithms
        1. 6.2.1.1Filtering for capable Payment Apps
      2. 6.2.2Extension Points
        1. 6.2.2.1Selection of Payment App
        2. 6.2.2.2Passing the Payment Request to the Payment App
  7. A.References
    1. A.1Normative references

1. Design Decisions

1.1 Improve the payment experience for payers

Payments on the Web today are very payee-centric. The entire payment process is driven by the payee, usually an online merchant, payment gateway in php presents the user with one or more ways to pay. In the majority of cases the user provides a static credential (such as the details of a credit card) back to the merchant or is redirected to a digital wallet service where they approve the transaction.

Unless the user is able to find a digital wallet that is supported by all of the payee’s they wish to pay they will never have a standardized payment experience and the majority of the time that experience is driven by the payee.

Further, there is no standardized mechanism to estabilsh a bi-directional channel of communcation between the payer (or their service provider) and the payee (or their service provider) which could be used to exchange payment details and credentials in a more secure manner or negotiate the use of an entirely new payment method.

To remedy this one-sided situation this architecture proposes a new standardized component, the payment app, that is used by the payer to handle payment requests.

1.2 Protect payer privacy

Facilitating an autonomous process for matching the payment methods that the payer supports with those that the payee supports seems trivial on the surface, however it is also important to ensure that the architecture doesn’t allow for user data to be leaked to the payee system.

To protect the payer’s privacy the architecture defines the role of a payment mediator that sits between the payer and payee mediating the initial handshake between them and conencting the requesting payee system with an appropriate payer payment app.

2. Definitions

In this document we use the following terms with the implied definition as stated below:PayerA payer is implied to mean any system operated by (or on behalf of) the payer. This includes systems operated by the payer’s payment services provider.PayeeA payee is implied to mean any system operated by (or on behalf of) the payee. This includes systems operated by the payee’s payment services provider.Payment RequestA payment request is a request from a payee to be paid. It contains the details of what to pay and how the payment can be made. How the payment can be made is specified as a list of payment method identifiers. The payment request MUST conatin all of the payment method data required for each payment methodidentified in it’s set of supported payment methods.Payment ResponseA payment response is a response to a payment request (normally the result of processing by a payment app). The content of a payment response will be dependant on how the payment is being processed.Payment AppA payment app is a component that fulfills the conformance criteria specified for a payment app in this specification.Payment MediatorA payment mediator is a component that fulfills the conformance criteria specified for a payment mediator in this specification.Payment MethodA payment method defines how a payment request must be processed. It defines the format and content of the payment request and payment response messages that are exchanged with a payment app. A payment app must be able to process payment requests for one or more payment methods.Payment Method DataPayment requests and payment responses have a limited set of data elements that are common to all payments, irresepective of the payment method. However payment methods can define additional custom data that is specific to that payment method. This custom data is called payment method data.Payment Method SpecificationA payment method specification defines what payment method data is required inpayment requests and payment responses for a particular payment method. It also lists the payment method identifiers that are defined for that payment method.Payment Method IdentifiersPayment Method Identfiers are defined in [METHOD-IDENTIFIERS]

3. Conformance

As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.

The key words MAYMUST, and SHOULD are to be interpreted as described in [RFC2119].

This specification defines two classes of products:Payment App

payment app MUST behave as described in this specification in order to be considered conformant. In this specification, payment app means a software component that exercises the role of a payment app as described in the section Roles.

An informative summary of the required capabilities of a payment app are:

  • It makes available a list of the payment methods for which it is able to process payment requests.
  • It accepts and processes payment requests that conform to the requirements for one of the payment methods it supports.
  • It returns payment responses that conform to the requirements of one of the payment methods it supports and one of the payment methods that was listed as supported in the original request.

Payment apps MAY implement algorithms given in this specification in any way desired, so long as the end result is indistinguishable from the result that would be obtained by the specification’s algorithms.Payment Mediator

payment mediator MUST behave as described in this specification in order to be considered conformant. In this specification, payment mediator means a software component that exercises the role of a payment mediator as described in the section Roles.

A summary of the required capabilities of a payment mediator are:

  • It maintains a record of the available payment apps that it is able to pass payment requests to.
  • It maintains a record of the payment methods that each of these payment apps supports.
  • It accepts payment requests and, using algorithms, user input or a combination of the two, determines which payment app to pass the payment request on to.
  • It accepts payment responses back from the payment app and passes these on to the originator of the corresponding payment request.

Payment mediators MAY implement algorithms given in this specification in any way desired, so long as the end result is indistinguishable from the result that would be obtained by the specification’s algorithms.

4. Payment Request Flow

The flow of a payment request is shown in the diagram below:

In this flow, any system or system component playing the role of payment app or payment mediator is assumed to be controlled by the payer. The payment request originates from the payee and is always processed first by a payment mediator.

It’s quite possible that a single component may fulfil both roles or even that the system only supports a singlepayment app, which is always used by default, and therefor the role of the payment mediator is reduced to simply proxying the request and response between the payee system and the payment app.

5. Payment Methods

Each payment app will support one or more payment methods. A payment method is a system and set of rules for processing a payment request. This architecture is designed to support the broadest possible array of payment methods or payment gateway in php. When a payee accepts a given payment method, the assumption is that the payee will know how to process the payment method data returned by the payment app for that payment method.

Each payment method is identified by a payment method identifier. The payment mediator compares payment method identifiers in the payment request to those, known to be enabled in the available payment apps to determine which payment apps to offer the payer to use to process the payment request.

Payment method identifiers support distributed extensibility, meaning that there is not a central machine-readable registry to discover or register payment methods. Rather, any perosn or entity can define a new payment methodas long as they publish a payment method specification that defines:

  1. How a payment app should process a payment request.
  2. What payment method data is expected to be included in the request.
  3. What payment method data is expected to be included in the response.
  4. What payment method identifiers identify this payment method.

6. Roles

The following are the key roles within this architecture.

6.1 Payment App

payment app is a component able to process a payment request and return a payment response . Banks, merchants, mobile operators, and anyone else who wants to, will make these available. User agents are also likely to take on the role of a paid app or payment gateway in php, storing payment related information on behalf of the user, as they already do today with passwords, and using this to process certain payment requests. It is expected that paid apps will increase security and privacy by giving users more control over what they share over the network. Payment apps will distinguish themselves through the features and services they provide beyond the required capabilities described here, for example by offering strong user authentication, loyalty program integration, back-channel communications with the merchant for fraud analytics, and so on.

Payment apps should be implementable on desktops, mobile devices, televisions, embedded devices and other devices and operating systems. Payment apps may even be implemented in contexts where they do not have direct access to the Web since they payment app receives payment requests from the payment mediator.

As part of processing a payment request or payment gateway in php, a payment app MAY render a user interface and/or take advantage of one or more other IO channels for payer or third-party interaction (e.g., with a payment service provider). In some cases, and where supported by a payment method, a payment app may be able to facilitate a payment without immediate user interaction based on prior payer consent. (Example: A Web of Things scenario where the payer is a machine capable of executing payments in accordance with predefined rules and logic).

The composition of a payment gateway in php will be platform and deployment dependent and many aspects of how the payment app or payment gateway in PHP communicates with the host platform, interacts with the payer and payee, and communicates with third-party services will be determined by the implementer who provides the execution environment for the app, the channels supported by the chosen payment gateway in PHP and the capabilities of the payment mediator, that mediates interactions between the payment app and the payee.

6.1.1 Supported vs Enabled Payment Methods

Payment apps support one or more payment methods. Support for a payment method implies that the app SHOULD be able to process a payment request that conforms to the rules defined in the payment method specification for that payment method and SHOULD be able to return a valid payment response.

However, sometimes an app will be designed to support a specific payment method but the app will not have been configured to process payments using that payment method. This may be due to some payment method data that the payer needs to configure such as a user credential or payment instrument details. In that case the payment app is said to support the payment method but that payment method is not enabled.

For example, a payment app may be capable of processing a basic credit card payment that simply returns the card details in the payment response. Unless the user has configured the payment app with the card details or is able to provide these at the time of processing, that method is not enabled.

If a payment app defines a payment method as enabled then it MUST be able to process any payment request that lists that that payment method as supported and is formatted correctly according to the payment method specification for that payment method.

Payment mediators should only consider enabled payment methods when evaluating if a payment app is appropriate to process a given payment request.

6.1.2 Algorithms

The following algorithms must be implemented by all conforming payment apps:

6.1.2.1 Processing Payment Requests

The most important capability of a payment app is it’s ability to process a payment request and return a valid payment response.

The steps for processing a payment request are given by the following algorithm. The algorithm takes a valid payment request (request) as input and returns a valid payment response (response).

  1. Let methods be an empty list.
  2. Let enabled methods be the list of payment method identifiers for the payment methods that are currently enabled in the app.
  3. Let candidate methods be the list of payment method identifiers listed as supported in request.
  4. If enabled methods is empty throw an exception.
  5. For each unique candidate payment method identifier in candidate methods
    1. If no identifier, that is an exact string match for candidate payment method identifier, is found in enabled methods, move onto the next item if there are any left.
    2. Append candidate payment method identifier to methods.
  6. If methods is empty throw an exception.
  7. Let selected payment method be the result of selecting a payment method with methods as input.
  8. Let response be the result of performing payment method specific processing with request and selected payment method as input.
6.1.2.2 Returning a list of enabled Payment Methods

In order for a payment mediator to know if it should consider passing a payment request to a payment app it must know which payment methods are enabled in that app.

The payment app MUST provide a mechanism for external components to get a list of enabled payment methods. The return value MUST be a set of unique payment method identifiers and the payment app MUST be capable of performing payment method specific processing for all payment methods identified by the identifiers in that set.

6.1.3 Extension Points

The following extension points are defined for implemntation specific processing.

6.1.3.1 Selecting a Payment Method

A payment app should implement a mechanism for selecting a payment method which takes as input a list of payment method identifiers and returns a single payment method identifier. This mechanism MAY include user input, selection based on a custom algorithm or even configured defaults.

6.1.3.2 Performing Payment Method Specific Processing

A payment app that claims to have a particular payment method enabled must provide a mechanism for performing payment method specific processing for that payment method.

The input to this process will be a complete payment request and the payment method identifier of the payment method that should be used to process the request. The payment app SHOULD expect the request to contain any payment method data required to complete the processing of the request.

The specific behaviour of the payment app during this processing MAY include user interaction or communication with other systems or components. This will be defined, either explicitly or implicitly, in the appropriate payment method specification. The payment app MUST return a payment response that contains the appropriate payment method data for the payment method identifed by the payment method identifier passed as input to the process.

6.2 Payment Mediator

The payment mediator sits between the payee’s system and the payer’s payment app(s) and payment gateway in php performs a number of functions:

  1. It determines which payment apps can be used to fulfill a payment request given the payee’s accepted payment methods.
  2. It helps the payer choose one (typically with explicit interaction or prior consent).
  3. It passes the payment request from the payee to the to the payer’s selected payment app and passes the payment response from the payment app back to the payee.

6.2.1 Algorithms

The following algorithms must be implemented by all conforming payment mediators:

6.2.1.1 Filtering for capable Payment Apps

payment mediator, upon receiving a payment request must compare the set of payment methods supported by the payee (as defined in the payment request) with the set of payment methods enabled in all available payment apps to which it has access and produce a list of payment apps that should be capable of processing the request.

The steps for getting a list of capable payment apps are given by the following algorithm. The algorithm takes a valid payment request (request) and the set of payment apps to which it is able to pass the payment request(apps) as input and returns a set of payment method identifiers.ISSUE: Determine the correct matching algorithm for payment method identifiers?There are a number of issues open against the browser API which need to be resolved to come to a conclusion on the correct algorithm to use in mathcing payment method identifiers.

6.2.2 Extension Points

The following extension points are defined for implemntation specific processing.

6.2.2.1 Selection of Payment App

Once the mediator has determined the set of payment apps that are able to process the payment request it must select one and pass the request to it.

The steps for selecting a payment app are implementation specific and MAY involve user interaction with the payer or may involve an algorithm that uses defaults and configuration data to make the selection.

The input to this process is an ordered list of payment apps and the output is a single payment app.

6.2.2.2 Passing the Payment Request to the Payment App

The payment mediator must have some mechanism of passing the payment request to the payment app that was selected following the steps for selecting a payment app.

The payment request MUST not be altered by the payment mediator.

A. References

A.1 Normative references

[METHOD-IDENTIFIERS]Payment Method Identifiers. Adrian Bateman; Zach Koch; Richard Barnes; Roy McElmurray.W3C Working Draft. URL: https://w3c.github.io/browser-payment-api/specs/method-identifiers.html[RFC2119]Key words for use in RFCs to Indicate Requirement Levels. S. Bradner. IETF. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

This document is licensed under a Creative Commons Attribution 3.0 License.

Latest editor’s draft:https://w3c.github.io/webpayments/architecture/index.htmlEditor:Adrian Hope-Bailie (Ripple)Version Controller: Github repository issues

Source credit : https://w3c.github.io/webpayments/proposals/architecture/#definitions

php database connection

mysql connect to database PHP

  • October 19, 2018

We realize that there are loads of instructional exercises (some authority as well) on the web about PHP and MySQL network. At that point for what reason am I posting it here? I am posting on the grounds that the vast majority of PHP database connection are not basic and straightforward and this is the reason we are doing it here. We expect that you have just introduced XAMPP in your System and running it utilizing XAMPP control board.

To associate php database connection database you have to know the accompanying essential things:

  1. Hostname.
  2. MySQL client name.
  3. MySQL secret phrase.

Associating with a database by means of PHP is a critical advance supposing that your content can’t interface with its database, your inquiries to the PHP database connection will come up short.

Description

A decent practice when utilizing databases is to set the username, the secret key and the database name esteems toward the start of the content code. In PHP database connection that you have to transform them later, along these lines you will most likely play out the change effectively.

This instructional exercise will be exceptionally helpful for you in the event that you are beginning to find out about PHP database connection advancement. Utilizing this instructional exercise, you will figure out how to utilize PHP to associate with MySQL database.

resource mysql_connect ([ string $server = ini_get(“mysql.default_host”) [, string $username = ini_get(“mysql.default_user”) [, string $password = ini_get(“mysql.default_password”) [, bool $new_link = FALSE [, int $client_flags = 0 ]]]]] )

Parameters

Server

The MySQL server. It can also include a port number. e.g. “hostname:port” or a path to a local socket e.g. “:/path/to/socket” for the localhost.

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

Username

The username. Default value is defined by mysql.default_user. In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used.

Password

The default value is defined by MySql.default_password. In SQL safe mode, this parameter is ignored and null password is used.

New_link

If a second call is made to mysql_connect() with the same arguments or php database connection , no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.
client_flags

The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE. Read the section about MySQL client constants for further information. In SQL safe mode, this parameter is ignored.

Examples

mysql_connect() example

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

mysql_connect() example using hostname:port syntax

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

mysql_connect() example using “:/path/to/socket” syntax

<?php
// we connect to localhost and socket e.g. /tmp/mysql.sock

// variant 1: omit localhost
$link = mysql_connect(':/tmp/mysql', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);


// variant 2: with localhost
$link = mysql_connect('localhost:/tmp/mysql.sock', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Visit our php operator article