# Quick-start

## Installation

Refer to the [installation page](/server-side/sdks/php/php-installation.md) for installation steps.

## Flagship Usage

### Start SDK

**Step 1:** Start the SDK by calling the [start](/server-side/sdks/php/php-reference.md#start-method) function of the [Flagship](/server-side/sdks/php/php-reference.md#flagship-class) class, in the most appropriate location for your application.

```php
require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

Flagship::start("<ENV_ID>", "<API_KEY>");
```

This starts the SDK in [DECISION-API](/server-side/concepts/decision-mode.md). More options are available in the [SDK configuration](/server-side/sdks/php/php-reference.md#flagshipconfig-class).

{% hint style="info" %}
👍 Good to know

Your **apiKey** and **environmentId** can be found in your Flagship account under Parameters > Environment & Security.
{% endhint %}

### Create a visitor

**Step 2:** Create a [visitor](/server-side/sdks/php/php-reference.md#visitorbuilder-class) using the [newVisitor](/server-side/sdks/php/php-reference.md#newvisitor-method) method from the [Flagship](/server-side/sdks/php/php-reference.md#flagship-class) instance. The visitor instance allows you to set relevant data for Flagship to make a decision, including: [Visitor ID](/server-side/glossary.md#visitor-id), [Visitor Context](/server-side/glossary.md#user-context), [GDPR Consent](/server-side/sdks/php/php-reference.md#newvisitor-method), and [Authentication status](/server-side/sdks/php/php-reference.md#authenticate-method).

For example, if you want to enable a specific feature for all your `VIP` visitors, you'll need to add this data as an attribute into the visitor context (key-value pair): `isVIP: true`. Based on your targeting criteria defined in your use-case (`isVIP === true`), Flagship will make the decision and show your feature to visitors that have `isVIP` in their context and for which `isVIP` is equal to `true`.

```php
require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

Flagship::start("<ENV_ID>", "<API_KEY>");

$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
        ->setContext(["isVip" => true])
        ->build();
```

### Getting Flags

**Steps 3, 4, 5, and 6** involve **fetching flags from Flagship**, **retrieving your flag**, **reading your flag's value.** , **and send hits.**

First, fetch flags from the [Flagship platform](https://app.flagship.io/login) using the [fetchFlags](/server-side/sdks/php/php-reference.md#fetchflags-method) method. You can ensure that your flags are fetched and ready to be used by using an `await` statement, a `then` function, or an `event listener`.

Then, use the [getFlag](/server-side/sdks/php/php-reference.md#getflag-method) method of the [**visitor instance**](/server-side/sdks/php/php-reference.md#visitorinterface) to retrieve a specific [flag](/server-side/sdks/php/php-reference.md#flaginterface) object based on the key provided. This object includes methods to retrieve the flag value, access flag metadata, expose the flag, verify the flag's existence, and get the flag status.

```typescript
require __DIR__ . '/vendor/autoload.php';

use Flagship\Flagship;

// Step 1: start the SDK
Flagship::start("<ENV_ID>", "<API_KEY>");

//Step 2: Create a visitor
$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
        ->setContext(["isVip" => true])
        ->build();

//Step 3: Fetch flag from the Flagship platform 
$visitor->fetchFlags();

//Step 4: Retrieves a flag named "displayVipFeature"
$flag = $visitor->getFlag("displayVipFeature");

//Step 5: Returns the flag value ,or if the flag does not exist, it returns the default value "false"
echo "flag value:". $flag->getValue(false);

//Step 6: Batch all the collected hits and send them
Flagship::close();
```

{% hint style="info" %}
👍 Good to know

* By default, the SDK assumes that the visitor has been exposed to the flag when `flag.getValue()` is called, so it sends the flag exposure hit to flagship server. However, this behavior can be changed. [Click here for more details](/server-side/sdks/php/php-reference.md#getvalue-method).
* Whenever your app (script) is about to **terminate** or **crash**, you should call [`Flagship::close()`](/server-side/sdks/php/php-reference.md#close-method) to batch all the collected hits and send them.\\
  {% endhint %}

### Tracking hits

**Step 7:** Send hits to Flagship using the [sendHit](/server-side/sdks/php/php-reference.md#sendhit-method) method of the [**visitor instance**](/server-side/sdks/php/php-reference.md#visitorinterface). These hits help validate your objectives (KPIs) set up in your campaign.

```jsx
// ... other code

$visitor->sendHit(new Event(EventCategory::ACTION_TRACKING, "add-to-cart"));

//Step 6: Batch all the collected hits and send them
Flagship::close();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.abtasty.com/server-side/sdks/php/php-quick-start.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
