Cookie Consent by Free Privacy Policy Generator 📌 PHP 8 News: Union Types and Mixed Types

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 PHP 8 News: Union Types and Mixed Types


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Union Types

PHP 8 introduced Union Types as a significant improvement in how we can declare data types in PHP. Prior to PHP 8, type declaration was limited to a single type or allowed only for the acceptance of null (using the ? operator). With Union Types, it's now possible to specify that a variable can have one of several different types.

The basic syntax for declaring Union Types involves using the vertical bar (|) between the desired types. Here's a simple example:

function sum(int|float $number): int|float {
    // ...
}

In this example, the sum function accepts a parameter $number that can be of type int or float, and the function can return a value of type int or float.

Union Types are useful when working with nullable values. Prior to PHP 8, you would use ?Type to indicate that a variable could be of type Type or null. Now, with Union Types, you can use Type|null to express the same idea more explicitly.

function sum(?int $value): int|null {
    // ...
}

Union Types, improving code readability, facilitating maintenance, and making type checking more robust. This is especially useful in large projects or situations where type clarity and precision are crucial.

Mixed Types

The mixed type is a dynamic and flexible way to handle variables whose types may change during the execution of a script. It essentially allows a variable to hold values of any type without imposing strict type constraints.

function processValue(mixed $data): mixed {
    // Code to process $data
}

In this example, the processValue function accepts a parameter $data of type mixed, and it can return a value of any type (mixed).

Beware that excessive use of mixed can diminish the benefits of strong typing and impair the ability of static analysis tools to detect possible errors. It is generally recommended to prefer more specific types when possible and to reserve the use of mixed for situations where the type cannot be precisely determined.

It's worth noting that mixed is implicitly used when a type declaration is omitted or when the @var PHPDoc annotation is used without specifying a type.

// Implicit use of mixed when type declaration is omitted
function exampleFunction($variable) {
    // $variable is of type mixed
}

/** @var mixed $dynamicVariable */
$dynamicVariable = getDynamicValue();
...



📌 PHP 8 News: Union Types and Mixed Types


📈 54.74 Punkte

📌 Bundestagswahl 2017: Union und SPD verlieren stark - Union stärkste, AfD drittstärkste Kraft


📈 25.36 Punkte

📌 Bundestagswahl 2017: Union und SPD verlieren stark - Union vorne, SPD in der Opposition, AfD drittstärkste Kraft


📈 25.36 Punkte

📌 YouTubers Union Teams With Actual Labor Union To Pressure YouTube


📈 25.36 Punkte

📌 Union Berlin gegen Royale Union Saint-Gilloise gratis im Live-Stream: So seht ihr das Spiel


📈 25.36 Punkte

📌 Union Berlin gegen Royale Union Saint-Gilloise im Live-Stream: So seht ihr die Europa League-Partie gratis


📈 25.36 Punkte

📌 Union Organizers Plan Class Action Lawsuit Against Apple for Union-Busting Tactics


📈 25.36 Punkte

📌 Union Berlin gegen Union Saint-Gilloise gratis im TV und Live-Stream: So seht ihr das Europa League-Achtelfinale


📈 25.36 Punkte

📌 Union-Saint-Gilloise gegen 1.FC Union Berlin gratis im TV und Live-Stream: So seht ihr das Europa League-Achtelfinale


📈 25.36 Punkte

📌 Exploring the Power of Union and Intersection Types in TypeScript


📈 24.24 Punkte

📌 Windows Mixed Reality: Warum Microsoft seine VR-Brillen "Mixed Reality" nennt


📈 23.21 Punkte

📌 Reported Mixed Feelings on Mixed Reality


📈 23.21 Punkte

📌 TypeScript 3.5 verbessert das Prüfen von Union Types


📈 22.48 Punkte

📌 What are Discriminated union types?


📈 22.48 Punkte

📌 How to Simplify Your Code with TypeScript Discriminated Union Types


📈 22.48 Punkte

📌 Understanding Value Types and Reference Types in C#


📈 21.37 Punkte

📌 Apple VR and mixed reality headset: Rumors and news


📈 20.7 Punkte

📌 Containous Traefik up to 1.7.11 types/types.go API Request information disclosure


📈 19.6 Punkte

📌 Data Types: 7 Key Data Types


📈 19.6 Punkte

📌 Infer Types to Avoid Explicit Types


📈 19.6 Punkte

📌 Cast Various Types into Decimal Types in SQL


📈 19.6 Punkte

📌 ⚛️ Demystifying React's Types: Ref types


📈 19.6 Punkte

📌 Good News On the Apple Front, Mixed Reality and Real Life Saving Features


📈 18.93 Punkte

📌 Falscher Schulz-Tweet: Junge Union macht Wahlkampf mit Fake-News


📈 18.24 Punkte

📌 They say the tooth will set you free... so Brit dentist trade union tells members: 'Bad news; we've been hacked'


📈 18.24 Punkte

📌 Fake-News: Junge Union darf gefälschten Schulz-Tweet nicht mehr verbreiten


📈 18.24 Punkte

📌 Mixed news about the electronics supply chain


📈 17.17 Punkte

📌 Mixed news about the electronics supply chain


📈 17.17 Punkte

📌 Mixed news about the electronics supply chain


📈 17.17 Punkte

📌 Set Theory in MySQL 8.0: UNION and Now INTERSECT and EXCEPT


📈 16.21 Punkte

📌 Movie Portal Script 7.36 /artist-display.php act Union sql injection


📈 16.2 Punkte

📌 Movie Portal Script 7.36 /movie.php f Union sql injection


📈 16.2 Punkte

📌 Programmiersprache: PHP 8 bekommt JIT-Compiler und Union Type


📈 16.2 Punkte











matomo