M
M
Maxim Tsyplenkov2021-06-28 02:27:10
MODX
Maxim Tsyplenkov, 2021-06-28 02:27:10

How to check for a variable?

Hello.
There is this code:

<?php
$videolink = $modx->resource->getTVValue('12');
$itemArray  = explode(' ', $videolink);
foreach ($itemArray as $item){
    $ytarray=explode("/", $item);
    $ytendstring=end($ytarray);
    $ytendarray=explode("?v=", $ytendstring);
    $ytendstring=end($ytendarray);
    $ytendarray=explode("&", $ytendstring);
    $ytendarray=explode(",", $ytendstring);
    $ytcode=$ytendarray[0];
    echo "
<h2>Видео</h2>
<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$ytcode\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>
";
}


How to make sure that if the $ytcode variable returns an empty response, the code is not published:
<h2>Видео</h2>
<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$ytcode\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vitaly_74, 2021-06-28
@vitaly_74

if(!is_null($ytcode)){
echo "
<h2>Видео</h2>
<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$ytcode\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>
";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question