bool $isTest
имеет прямое отношение к методу\классу, стоит вызывать его изнутри, если это чисто внешняя проверка не имеющая прямого отношения - стоит вызывать извне. Чисто чтобы не смешивать логику. <?php
class SomeClass
{
private string $title = '';
private string $content = '';
private array $error = [];
public function create(): void
{
if (empty($this->error)) {
$this->add();
}
echo $this->getStatus();
}
private function add(): void
{
$query = "insert into posts set title = :title, content = :content, created = now()";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(":title", $this->title);
$stmt->bindParam(":content", $this->content);
if (!$stmt->execute()) {
$this->error[] = 'Запись в базу не прошла!';
}
}
public function setTitle(string $string): void
{
if (empty($string)) {
$this->error[] = 'Поле title не заполнено!';
} elseif (strlen($string) <= 5) {
$this->error[] = 'Поле title меньше 5 символов!';
} else {
$this->title = htmlspecialchars(strip_tags($string));
}
}
public function setContent(string $string): void
{
if (empty($string)) {
$this->error[] = 'Поле content не заполнено!';
} else {
$this->content = htmlspecialchars(strip_tags($string));
}
}
private function getStatus(): string
{
if (empty($this->error)) {
return '<div class="alert alert-success">Успешно!</div>';
}
$string = '';
foreach ($this->error as $error) {
$string .= "<li>$error</li>";
}
return "<div class=\"alert alert-danger\"><ol>$string</ol></div>";
}
}
...
if (isset($_GET['title']) && isset($_GET['text'])) {
$c = new SomeClass();
$c->setTitle($_GET['title']);
$c->setContent($_GET['text']);
$c->create;
}
...
select `id` from table_1 where `id` not in (select distinct `UID` from table_2)
function php_func(){
echo "Stay Safe";
}
function clickMe(){
var result ="<?php php_func(); ?>"
document.write(result);
}
<button onclick="clickMe()"> Click </button>
$array = [1, -20, 'tango', 'whiskey',];
/* echo $array[0]; // 1
* echo $array[1]; // -20
* echo $array[2]; // tango
* echo $array[3]; // whiskey
*/
$array = ['foo' => 'bar', 'baz' => 27,]
/* echo $array[0]; // bar
* echo $array[1]; // 27
* echo $array['foo']; // bar
* echo $array['baz']; // 27
*/
$opt = $_SERVER['QUERY_STRING'] ?? null;
if ($opt == 'all') {
$opt = null;
}
function l($link = 'all'): string
{
global $opt;
return $link != $opt ? " <a class='head' href='?$link'>[$link]</a>" : '';
}
echo 'Some Links:' . l() . l('link1') . l('link2') . l('link3');