I'm doing an AJAX request and somehow a trailing newline is added somewhere.  

My PHP script echoes (or is supposed to echo) `'SUCCESS'` if the request succeeded, `'ERROR'` otherwise.  

But currently it returns: `'SUCCESS'` (i.e. `'SUCCESS\n'`).  

I saw that by adding an `alert("!" + msg + "!")` that showed the line break.

My AJAX call:

```
function addMedia() {
    var addMediaName = $("#addMediaName").val();
    var notif;
    if(addMediaName != ""){
        $.ajax({
            url : '../../controler/add/addMedia.php',
            type : 'POST',
            data : "mediaName="+ addMediaName,
            dataType : 'text',
            success: function(msg,data, settings){
                if(msg == 'SUCCESS'){
                    $.toaster({ priority : 'success', title : 'Success', message : 'Mode created' });
                } else {
                    $.toaster({ priority : 'warning', title : 'Failed', message : 'Mode already exists' });
                }
            },
        });
    }
}

```

My PHP controller:

```
<?php
include ('../../model/request/add.php');
if((include_once '../../model/request/add.php')===FALSE) exit('erreur include');
$mediaName = $_POST['mediaName'];
$mediaName = ucfirst(strtolower($mediaName));
$media = addMedia($mediaName);
?>

```

And the `addMedia` function:

```
function addMedia($mediaName)
{
    global $conn;
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $sql = 'INSERT INTO media (mediaName) VALUES 
        ("'.$mediaName.'")';
        $conn->exec($sql);
        echo 'SUCCESS';
    } catch(PDOException $e) {
        echo 'ERREUR';
    }
}

```

Any idea where that newline is coming from and how I can fix it?
['https://Stackoverflow.com/questions/30347289', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4915360/']
That is most likely caused by the end of the PHP file, where a newline follows the `?>`.

Now while it is possible to `die`/`exit` at a previous point, I consider it a much cleaner solution to simply remove the `?>`.  

The closing tag is not required by PHP, and it is usually even considered better practise not to use it, see [this SO question](https://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag).
add [die()](http://www.w3schools.com/php/func_misc_die.asp) or [exit()](http://www.w3schools.com/php/func_misc_exit.asp) after 

```
echo 'SUCCESS'; die;    

```

or 

```
echo 'ERREUR'; die;

```

if you can't add `die` then code execute next line of code or in this case it will call 'view' in MVC.